{"id":319149,"date":"2021-03-07T03:00:39","date_gmt":"2021-03-07T03:00:39","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=319149"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=319149","title":{"rendered":"\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043a\u043e\u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0443\u044e \u043c\u043d\u043e\u0433\u043e\u0437\u0430\u0434\u0430\u0447\u043d\u043e\u0441\u0442\u044c \u043d\u0430 C#"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\">\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/5s\/8k\/dz\/5s8kdzthg3nclbsmaowvlqurjog.png\"><\/p>\n<p>  <\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0440\u0435\u0447\u044c \u0437\u0430\u0445\u043e\u0434\u0438\u0442 \u043e \u043c\u043d\u043e\u0433\u043e\u0437\u0430\u0434\u0430\u0447\u043d\u043e\u0441\u0442\u0438 \u0432 .Net, \u0442\u043e \u0432 \u043f\u043e\u0434\u0430\u0432\u043b\u044f\u044e\u0449\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f \u0432\u044b\u0442\u0435\u0441\u043d\u044f\u044e\u0449\u0430\u044f \u043c\u043d\u043e\u0433\u043e\u0437\u0430\u0434\u0430\u0447\u043d\u043e\u0441\u0442\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b. \u041d\u043e \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0435\u0447\u044c \u043f\u043e\u0439\u0434\u0451\u0442 \u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 <strong>\u043a\u043e\u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u0439<\/strong> \u043c\u043d\u043e\u0433\u043e\u0437\u0430\u0434\u0430\u0447\u043d\u043e\u0441\u0442\u0438, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0432\u0441\u0435\u0433\u043e \u043e\u0434\u0438\u043d \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a.<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<p>\u0412\u043e\u0442 \u043d\u0430\u0448\u0430 \u043f\u0440\u043e\u0441\u0442\u0435\u043d\u044c\u043a\u0430\u044f \u0437\u0430\u0433\u043e\u0442\u043e\u0432\u043a\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">static void Main() {     DoWork(&quot;A&quot;, 4);     DoWork(&quot;B&quot;, 3);     DoWork(&quot;C&quot;, 2);     DoWork(&quot;D&quot;, 1); }  static void DoWork(string name, int num) {     for (int i = 1; i &lt;= num; i++)     {         Console.WriteLine($&quot;Work {name}: {i}&quot;);     }     Console.WriteLine($&quot;Work {name} is completed&quot;); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0421\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e 4 \u0440\u0430\u0437\u0430 \u0438 \u0432\u044b\u0432\u043e\u0434\u0438\u0442 \u0441\u0438\u043c\u0432\u043e\u043b\u044b A, B, C, D \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437. \u0421\u0438\u043c\u0432\u043e\u043b\u044b, \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0442\u0430\u043a\u0436\u0435 \u0432\u044b\u0432\u043e\u0434\u044f\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0438 \u043d\u0430\u0448\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0437\u0434\u0435\u0441\u044c \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0431\u044b \u043e\u043d\u0438 \u0432\u044b\u0432\u043e\u0434\u0438\u043b\u0438\u0441\u044c \u043f\u043e\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e, \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0441\u0438\u043b\u044c\u043d\u043e \u043d\u0435 \u043c\u0435\u043d\u044f\u044f \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u0438 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043f\u043e\u0442\u043e\u043a\u0438.<\/p>\n<p>  <\/p>\n<p>\u0417\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u043a\u0430\u043a \u0431\u044b \u043d\u0430\u043c\u0435\u043a\u0430\u0435\u0442, \u0447\u0442\u043e \u0442\u0443\u0442 \u043d\u0443\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0443\u044e \u043c\u043d\u043e\u0433\u043e\u0437\u0430\u0434\u0430\u0447\u043d\u043e\u0441\u0442\u044c, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0439 \u043a\u043e\u0434 \u0432\u0440\u0435\u043c\u044f \u043e\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u0442, \u0447\u0442\u043e: \u201c\u042f \u043f\u043e\u043a\u0430 \u0440\u0430\u0431\u043e\u0442\u0443 \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u044e, \u0438 \u043a\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u0435\u0449\u0435 \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u043f\u043e\u0442\u043e\u043a\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u0432\u043e\u0438 \u0434\u0435\u043b\u0430, \u043d\u043e \u044f \u043d\u0430\u0434\u0435\u044e\u0441\u044c, \u0447\u0442\u043e \u0440\u0430\u043d\u043e \u0438\u043b\u0438 \u043f\u043e\u0437\u0434\u043d\u043e, \u043c\u043d\u0435 \u044d\u0442\u043e \u043f\u043e\u0442\u043e\u043a \u0432\u0435\u0440\u043d\u0443\u0442 \u043e\u0431\u0440\u0430\u0442\u043d\u043e, \u0432\u0435\u0434\u044c \u0443 \u043c\u0435\u043d\u044f \u0435\u0449\u0435 \u043a\u0443\u0447\u0430 \u0434\u0435\u043b\u201d. <\/p>\n<p>  <\/p>\n<p>\u0412\u043e\u043f\u0440\u043e\u0441: &quot;\u041a\u0430\u043a\u0430\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f C# \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u0435\u0440\u0432\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u043c\u0435\u0442\u043e\u0434\u0430 \u043d\u0430 \u043a\u0430\u043a\u043e\u0435-\u0442\u043e \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u043d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f?&quot; \u041f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e! <strong>await<\/strong>! \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u043d\u0430\u0448 \u043c\u0435\u0442\u043e\u0434 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u043c \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043c <strong>await <\/strong>\u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0432\u043e\u0434\u0430 \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0433\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">static async ValueTask DoWork(string name, int num) {     for (int i = 1; i &lt;= num; i++)     {         Console.WriteLine($&quot;Work {name}: {i}&quot;);         await \/*Something*\/     }     Console.WriteLine($&quot;Work {name} is completed&quot;); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u042d\u0442\u043e\u0442 <strong>await<\/strong> \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043c\u0435\u0442\u043e\u0434 \u0440\u0435\u0448\u0438\u043b \u043f\u0440\u0435\u0440\u0432\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u0434\u0440\u0443\u0433\u0438\u0435 \u0432\u044b\u0437\u043e\u0432\u044b \u0442\u043e\u0436\u0435 \u0441\u043c\u043e\u0433\u043b\u0438 \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0441\u0432\u043e\u0438 \u0441\u0438\u043c\u0432\u043e\u043b\u044b. \u041d\u043e \u0447\u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 await-\u0438\u0442\u044c? <strong>Task.Delay()<\/strong>, <strong>Task.Yield()<\/strong> \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0442, \u0442\u0430\u043a \u043a\u0430\u043a \u043e\u043d\u0438 \u043f\u043e\u0434\u0440\u0430\u0437\u0443\u043c\u0435\u0432\u0430\u044e\u0442 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043d\u0430 \u0434\u0440\u0443\u0433\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u0438. \u0422\u043e\u0433\u0434\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0432\u043e\u0439 \u043a\u043b\u0430\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441 <strong>await<\/strong>, \u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u043d\u0438\u0447\u0435\u0433\u043e \u043e\u0431\u0449\u0435\u0433\u043e \u0441 \u043c\u043d\u043e\u0433\u043e\u043f\u043e\u0442\u043e\u0447\u043a\u043e\u0439. \u041d\u0430\u0437\u043e\u0432\u0435\u043c \u0435\u0433\u043e <strong>CooperativeBroker<\/strong>:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">private class CooperativeBroker : ICooperativeBroker {     private Action? _continuation;      public void GetResult()          =&gt; this._continuation = null;      public bool IsCompleted          =&gt; false;\/\/Preventing sync completion in async method state machine      public void OnCompleted(Action continuation)     {         this._continuation = continuation;         this.InvokeContinuation();     }      public ICooperativeBroker GetAwaiter()          =&gt; this;      public void InvokeContinuation()          =&gt; this._continuation?.Invoke(); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 C# \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0432 \u0432\u0438\u0434\u0435 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0433\u043e \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0430, \u043a\u0430\u0436\u0434\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0432\u044b\u0437\u043e\u0432\u0443 <strong>await<\/strong> \u0432\u043d\u0443\u0442\u0440\u0438 \u044d\u0442\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430. \u041a\u043e\u0434 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \u0432\u0438\u0434\u0435 \u0434\u0435\u043b\u0435\u0433\u0430\u0442\u0430 <strong>continuation<\/strong> \u0432 \u043c\u0435\u0442\u043e\u0434 <strong>OnCompleted<\/strong>. \u0412 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0439 \u0436\u0438\u0437\u043d\u0438 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e <strong>continuation <\/strong> \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u043d, \u043a\u043e\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0430 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f, \u043d\u043e \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043d\u0435\u0442 \u0438 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043d\u0430\u0434\u043e \u0431\u044b \u0431\u044b\u043b\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u044d\u0442\u043e <strong>continuation <\/strong> \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e, \u043d\u043e \u0442\u043e\u0433\u0434\u0430 \u043c\u0435\u0442\u043e\u0434\u044b \u043e\u043f\u044f\u0442\u044c \u0431\u0443\u0434\u0443\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0430 \u043c\u044b \u044d\u0442\u043e\u0433\u043e \u043d\u0435 \u0445\u043e\u0442\u0438\u043c. \u041b\u0443\u0447\u0448\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u043c \u044d\u0442\u043e\u0442 \u0434\u0435\u043b\u0435\u0433\u0430\u0442 \u043d\u0430 \u0431\u0443\u0434\u0443\u0449\u0435\u0435 \u0438 \u0434\u0430\u0434\u0438\u043c \u043f\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u043c \u0432\u044b\u0437\u043e\u0432\u0430\u043c. \u0427\u0442\u043e\u0431\u044b \u0431\u044b\u043b\u043e \u0433\u0434\u0435 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0434\u0435\u043b\u0435\u0433\u0430\u0442\u044b \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043a\u043b\u0430\u0441\u0441 <strong>CooperativeContext<\/strong>:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">private class CooperativeBroker {     private readonly CooperativeContext _cooperativeContext;      private Action? _continuation;      public CooperativeBroker(CooperativeContext cooperativeContext)         =&gt; this._cooperativeContext = cooperativeContext;      ...      public void OnCompleted(Action continuation)     {         this._continuation = continuation;         this._cooperativeContext.OnCompleted(this);     }  }  public class CooperativeContext {     private readonly List&lt;CooperativeBroker&gt; _brokers =          new List&lt;CooperativeBroker&gt;();      void OnCompleted(CooperativeBroker broker)     {         ...     } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0433\u0434\u0435 \u043c\u0435\u0442\u043e\u0434 <strong>OnCompleted<\/strong> \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0438 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0432\u0435\u0447\u0430\u0442\u044c \u0437\u0430 \u043f\u043e\u043e\u0447\u0435\u0440\u0435\u0434\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u043c\u0435\u0442\u043e\u0434\u043e\u0432:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">private void OnCompleted(CooperativeBroker broker) {     \/\/\u041f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0432\u044b\u0437\u043e\u0432\u044b \u0434\u0435\u043b\u0435\u0433\u0430\u0442\u043e\u0432 \u043f\u043e\u043a\u0430 \u0432\u0441\u0435 \u0431\u0440\u043e\u043a\u0435\u0440\u044b \u043d\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u044b.     if (this._targetBrokersCount == this._brokers.Count)     {         var nextIndex = this._brokers.IndexOf(broker) + 1;         if (nextIndex == this._brokers.Count)         {             nextIndex = 0;         }          this._brokers[nextIndex].InvokeContinuation();     } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u043f\u0435\u0440\u0432\u043e\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u2013 \u0434\u0435\u043b\u0435\u0433\u0430\u0442\u044b \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u0432\u0441\u0435 \u0431\u0440\u043e\u043a\u0435\u0440\u044b \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043a\u043e\u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u044b \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 (<strong>_targetBrokersCount<\/strong> \u2014 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u043e\u0432). \u0415\u0441\u043b\u0438 \u043d\u0430\u0447\u0430\u0442\u044c \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0438\u0445 \u0441\u0440\u0430\u0437\u0443, \u0442\u043e \u0432\u044b\u0437\u043e\u0432\u044b \u043e\u043f\u044f\u0442\u044c \u043f\u043e\u0439\u0434\u0443\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0442\u0430\u043a \u043a\u0430\u043a \u043d\u0430\u0448 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c &quot;\u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435&quot; \u0432\u0441\u0435\u0445 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0441\u0440\u0430\u0437\u0443.<\/p>\n<p>  <\/p>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u043d\u0430\u0442\u044c \u043e\u0431\u0449\u0435\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u043e\u0432, \u0442\u043e \u043f\u0435\u0440\u0435\u043f\u0438\u0448\u0435\u043c \u044d\u0442\u0438 \u0432\u044b\u0437\u043e\u0432\u044b \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">static void Main() {     CooperativeContext.Run(         b =&gt; DoWork(b, &quot;A&quot;, 4),         b =&gt; DoWork(b, &quot;B&quot;, 3),         b =&gt; DoWork(b, &quot;C&quot;, 2),         b =&gt; DoWork(b, &quot;D&quot;, 1)     ); }  static async ValueTask DoWork(CooperativeBroker broker, string name, int num, bool extraWork = false) {     for (int i = 1; i &lt;= num; i++)     {         Console.WriteLine($&quot;Work {name}: {i}, Thread: {Thread.CurrentThread.ManagedThreadId}&quot;);         await broker;     }      Console.WriteLine($&quot;Work {name} is completed, Thread: {Thread.CurrentThread.ManagedThreadId}&quot;); }  public class CooperativeContext {     public static void Run(params Func&lt;CooperativeBroker, ValueTask&gt;[] tasks)     {         CooperativeContext context = new CooperativeContext(tasks.Length);         foreach (var task in tasks)         {             task(context.CreateBroker());         }          ...     }      ...      private int _targetBrokersCount;      private CooperativeContext(int maxCooperation)     {         this._threadId = Thread.CurrentThread.ManagedThreadId;         this._targetBrokersCount = maxCooperation;     }      ... }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u0440\u043e\u0434\u0435 \u0431\u044b \u043c\u043e\u0436\u043d\u043e \u0443\u0436\u0435 \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043d\u043e \u043e\u0441\u0442\u0430\u043b\u0430\u0441\u044c \u0435\u0449\u0435 \u043e\u0434\u043d\u0430 \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0430\u044f \u0434\u0435\u0442\u0430\u043b\u044c \u2013 \u0435\u0441\u043b\u0438 \u043e\u0434\u0438\u043d \u0438\u0437 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u043d\u044c\u0448\u0435 \u0434\u0440\u0443\u0433\u0438\u0445, \u0442\u043e \u043e\u043d \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434 OnCompleted \u0438 \u0432\u0441\u044f \u043d\u0430\u0448\u0430 \u0446\u0435\u043f\u043e\u0447\u043a\u0430 \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f. \u0427\u0442\u043e \u0431\u044b \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u0443 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e \u0444\u043e\u0440\u0441\u0438\u0440\u0443\u0435\u043c \u0432\u044b\u0437\u043e\u0432 \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0445\u0441\u044f &quot;\u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0439&quot;, \u0438 \u0437\u0430\u043e\u0434\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u043c \u043d\u0435\u043d\u0443\u0436\u043d\u044b\u0439 \u0443\u0436\u0435 \u0431\u0440\u043e\u043a\u0435\u0440:<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">public class CooperativeContext {     public static void Run(params Func&lt;ICooperativeBroker, ValueTask&gt;[] tasks)     {         CooperativeContext context = new CooperativeContext(tasks.Length);         foreach (var task in tasks)         {             task(context.CreateBroker());         }          \/\/ \u041f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0441\u044e\u0434\u0430 \u043a\u043e\u0433\u0434\u0430 \u043e\u0434\u0438\u043d \u0438\u0437 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d, \u043d\u043e \u043d\u0430\u0434\u043e         \/\/ \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u0442\u044c \u0438 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435         while (context._brokers.Count &gt; 0)         {             context.ReleaseFirstFinishedBrokerAndInvokeNext();         }     }      ...     private void ReleaseFirstFinishedBrokerAndInvokeNext()     {         \/\/ IsNoAction \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0442\u043e \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d         var completedBroker = this._brokers.Find(i =&gt; i.IsNoAction)!;          var index = this._brokers.IndexOf(completedBroker);         this._brokers.RemoveAt(index);         this._targetBrokersCount--;          if (index == this._brokers.Count)         {             index = 0;         }          if (this._brokers.Count &gt; 0)         {             this._brokers[index].InvokeContinuation();         }     }     }  private class CooperativeBroker : ICooperativeBroker {     ...     public bool IsNoAction         =&gt; this._continuation == null;     ... }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u043e\u0442 \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c (\u0447\u0443\u0442\u044c \u0443\u0441\u043b\u043e\u0436\u043d\u0438\u043c \u043d\u0430\u043c \u0437\u0430\u0434\u0430\u0447\u0443 \u0432\u0432\u0435\u0434\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0440\u0430\u0431\u043e\u0442\u0443):<\/p>\n<p>  <\/p>\n<pre><code class=\"cs\">static void Main() {     CooperativeContext.Run(         b =&gt; DoWork(b, &quot;A&quot;, 4),         b =&gt; DoWork(b, &quot;B&quot;, 3, extraWork: true),         b =&gt; DoWork(b, &quot;C&quot;, 2),         b =&gt; DoWork(b, &quot;D&quot;, 1)     ); }  static async ValueTask DoWork(     ICooperativeBroker broker,      string name,      int num,      bool extraWork = false) {     for (int i = 1; i &lt;= num; i++)     {         Console.WriteLine(                $&quot;Work {name}: {i}, Thread: {Thread.CurrentThread.ManagedThreadId}&quot;);         await broker;         if (extraWork)         {             Console.WriteLine(                    $&quot;Work {name}: {i} (Extra), Thread: {Thread.CurrentThread.ManagedThreadId}&quot;);             await broker;         }     }      Console.WriteLine(            $&quot;Work {name} is completed, Thread: {Thread.CurrentThread.ManagedThreadId}&quot;); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">Work A: 1, Thread: 1 Work B: 1, Thread: 1 Work C: 1, Thread: 1 Work D: 1, Thread: 1 Work A: 2, Thread: 1 Work B: 1 (Extra), Thread: 1 Work C: 2, Thread: 1 Work D is completed, Thread: 1 Work A: 3, Thread: 1 Work B: 2, Thread: 1 Work C is completed, Thread: 1 Work A: 4, Thread: 1 Work B: 2 (Extra), Thread: 1 Work A is completed, Thread: 1 Work B: 3, Thread: 1 Work B: 3 (Extra), Thread: 1 Work B is completed, Thread: 1<\/code><\/pre>\n<p>  <\/p>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e, \u0432\u0441\u0435 \u0432\u044b\u0437\u043e\u0432\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u043b\u0438\u0441\u044c \u0432 \u043e\u0434\u043d\u043e\u043c \u0438 \u0442\u043e\u043c \u0436\u0435 \u043f\u043e\u0442\u043e\u043a\u0435, \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u043b\u0438\u0441\u044c, \u0447\u0442\u043e\u0431\u044b \u0434\u0430\u0442\u044c \u043f\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0434\u0443\u0433 \u0434\u0440\u0443\u0433\u0443, \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u044f \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043c\u043d\u043e\u0433\u043e\u0437\u0430\u0434\u0430\u0447\u043d\u043e\u0441\u0442\u044c.<\/p>\n<p>  <\/p>\n<hr>\n<p>  <\/p>\n<p>\u0427\u0435\u0441\u0442\u043d\u043e \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u044f \u043d\u0435 \u043c\u043e\u0433\u0443 \u0441\u0445\u043e\u0434\u0443 \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u0442\u044c, \u0433\u0434\u0435 \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u043b\u0441\u044f \u0431\u044b, \u043d\u043e \u043d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u0442\u0430\u043a\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u043a\u0430 \u043d\u0430\u0439\u0434\u0435\u0442\u0441\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043b\u0443\u0447\u0448\u0435 \u0437\u043d\u0430\u0442\u044c, \u0447\u0442\u043e C# \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0434\u0435\u043b\u0430\u0442\u044c \u0438 \u0442\u0430\u043a\u043e\u0435.<\/p>\n<p>  <\/p>\n<p><a href=\"https:\/\/github.com\/0x1000000\/CooperativeMultitasking\/tree\/main\/CooperativeMultitasking\" rel=\"nofollow noopener noreferrer\">\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043d\u0430\u0439\u0442\u0438 \u043d\u0430 github.<\/a><\/p>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b\u0448\u0435\u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u0432\u0430\u043c \u0432\u0441\u0435 \u0436\u0435 \u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0432\u0430\u043c \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u0447\u0435\u0440\u0442\u043e\u0432\u0449\u0438\u043d\u043e\u0439, \u0442\u043e \u043c\u043e\u0433\u0443 \u043f\u043e\u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0441\u043b\u0443\u0448\u0430\u0442\u044c <a href=\"https:\/\/youtu.be\/_suxE9frTFA?t=992\" rel=\"nofollow noopener noreferrer\">\u043c\u043e\u0439 \u0434\u043e\u043a\u043b\u0430\u0434 \u043d\u0430 CLRium \u043f\u0440\u043e \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u0443\u044e \u043c\u0430\u0448\u0438\u043d\u0443 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0432 C#<\/a>. \u0422\u0430\u043c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043d\u043e \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 async\/await.<\/p>\n<p>  <\/p>\n<p>[Update] \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438 \u043e\u0442 <a href=\"https:\/\/habr.com\/ru\/users\/distortneo\/\" class=\"user_link\">DistortNeo<\/a>, \u043e\u043d \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442 \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/545844\/\"> https:\/\/habr.com\/ru\/post\/545844\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\">\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/5s\/8k\/dz\/5s8kdzthg3nclbsmaowvlqurjog.png\"><\/p>\n<p>  <\/p>\n<p>\u041a\u043e\u0433\u0434\u0430 \u0440\u0435\u0447\u044c \u0437\u0430\u0445\u043e\u0434\u0438\u0442 \u043e \u043c\u043d\u043e\u0433\u043e\u0437\u0430\u0434\u0430\u0447\u043d\u043e\u0441\u0442\u0438 \u0432 .Net, \u0442\u043e \u0432 \u043f\u043e\u0434\u0430\u0432\u043b\u044f\u044e\u0449\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f \u0432\u044b\u0442\u0435\u0441\u043d\u044f\u044e\u0449\u0430\u044f \u043c\u043d\u043e\u0433\u043e\u0437\u0430\u0434\u0430\u0447\u043d\u043e\u0441\u0442\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b. \u041d\u043e \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0435\u0447\u044c \u043f\u043e\u0439\u0434\u0451\u0442 \u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 <strong>\u043a\u043e\u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u0439<\/strong> \u043c\u043d\u043e\u0433\u043e\u0437\u0430\u0434\u0430\u0447\u043d\u043e\u0441\u0442\u0438, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0432\u0441\u0435\u0433\u043e \u043e\u0434\u0438\u043d \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-319149","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/319149","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=319149"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/319149\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=319149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=319149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=319149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}