{"id":275876,"date":"2016-03-10T17:35:03","date_gmt":"2016-03-10T14:35:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=275876"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=275876","title":{"rendered":"\u042d\u043c\u0443\u043b\u044f\u0446\u0438\u044f async\/await \u0432 Delphi"},"content":{"rendered":"<p>       \u0412 C# \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f async\/await. \u0414\u0430\u043b\u0435\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043a\u0430\u043a \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0433\u043e \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043d\u0430 Delphi.<a name=\"habracut\"><\/a><\/p>\n<p>  \u042f \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0433\u0430\u044e, \u0447\u0442\u043e \u0432\u044b \u0437\u043d\u0430\u043a\u043e\u043c\u044b \u0441 async\/await. \u0415\u0451 \u0443\u0434\u043e\u0431\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u0433\u0434\u0435 \u0443\u0447\u0430\u0441\u0442\u0438\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u0430 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f. \u041f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e, \u0430 \u0438\u0437 \u0432\u043d\u0435\u0448\u043d\u0435\u0433\u043e \u043c\u0438\u0440\u0430 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0441\u043e\u043e\u0431\u0449\u0430\u044e\u0442 \u043e\u0431 \u0435\u0451 \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u0438. \u0425\u043e\u0440\u043e\u0448\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u043c \u043c\u043e\u0436\u0435\u0442 \u0441\u043b\u0443\u0436\u0438\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u043d\u0430 \u0432\u0435\u0431 \u0441\u0435\u0440\u0432\u0435\u0440. \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u043d\u0430\u0448 \u0432\u0435\u0431 \u0441\u0435\u0440\u0432\u0435\u0440 \u0443\u043c\u0435\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0434\u0432\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438: \u0441\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0438 \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435.  <\/p>\n<pre><code class=\"cs\">        async Task&lt;int&gt; MakeAsyncTask_Plus(int aArg1, int aArg2, int aDurationMs)         {   \/\/ \u044d\u043c\u0443\u043b\u044f\u0446\u0438\u044f \u0432\u044b\u0437\u043e\u0432\u0430 \u043d\u0430 \u0432\u0435\u0431 \u0441\u0435\u0440\u0432\u0435\u0440             await Task.Delay(aDurationMs);             return aArg1 + aArg2;         }         async Task&lt;int&gt; MakeAsyncTask_Mult(int aArg1, int aArg2, int aDurationMs)         {   \/\/ \u044d\u043c\u0443\u043b\u044f\u0446\u0438\u044f \u0432\u044b\u0437\u043e\u0432\u0430 \u043d\u0430 \u0432\u0435\u0431 \u0441\u0435\u0440\u0432\u0435\u0440             await Task.Delay(aDurationMs);             return aArg1 * aArg2;         } <\/code><\/pre>\n<p>  \u041a\u043b\u0438\u0435\u043d\u0442 \u0445\u043e\u0447\u0435\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 (1+2)*(3+4). \u0422.\u043a. \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0441\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b, \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e:  <\/p>\n<pre><code class=\"cs\">        async void CalcAsync()         {             Task&lt;int&gt; aPlus1 = MakeAsyncTask_Plus(1, 2, 2000);      \/\/ 1             Task&lt;int&gt; aPlus2 = MakeAsyncTask_Plus(3, 4, 2000);      \/\/ 2               int aArg1 = await aPlus1;                                \/\/ 3             int aArg2 = await aPlus2;                                \/\/ 4               Task&lt;int&gt; aMult = MakeAsyncTask_Mult(aArg1, aArg2, 1000); \/\/ 5             int aRes = await aMult;                                  \/\/ 6             Log(string.Format(&quot;{0} * {1} = {2}&quot;, aArg1, aArg2, aRes));         } <\/code><\/pre>\n<p>  \u0414\u043e \u0441\u0442\u0440\u043e\u043a\u0438 &quot;\/\/3&quot;(\u043f\u0435\u0440\u0432\u043e\u0433\u043e await) \u043c\u0435\u0442\u043e\u0434 \u043e\u0442\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u043e\u0431\u044b\u0447\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c. \u041d\u0430 await \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0432\u044b\u0445\u043e\u0434 \u0438\u0437 \u043c\u0435\u0442\u043e\u0434\u0430, \u0430 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u0441\u044f (\u0434\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e await) \u043f\u043e \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438. \u0423\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0441\u0442\u0441\u044f \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u043e\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0438\u043b\u0438 \u0432 \u0434\u0440\u0443\u0433\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435 \u2014 \u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u0438. \u0412 C# \u044d\u0442\u043e \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044f\u043c\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430. \u0412 Delphi \u043f\u043e\u0445\u043e\u0436\u0435\u0433\u043e \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043d\u0430 \u043e\u0434\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0441\u0442\u0438\u0447\u044c \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 Fiber. \u0412\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0445\u043e\u0436\u0435:  <\/p>\n<pre><code class=\"delphi\">procedure TCalcAsync.Execute; var   aPlus1: TAsyncTask&lt;Integer&gt;;   aPlus2: TAsyncTask&lt;Integer&gt;;   aMult: TAsyncTask&lt;Integer&gt;;   aArg1, aArg2: Integer;   aRes: Integer; begin   aPlus1 := nil;   aPlus2 := nil;   aMult := nil;   try     aPlus1 := TAsyncTask_Plus.Create(Self, 1,{+}2, 2000{ms});     aPlus2 := TAsyncTask_Plus.Create(Self, 3,{+}4, 2000{ms});       aArg1 := aPlus1.Await;     aArg2 := aPlus2.Await;       aMult := TAsyncTask_Mult.Create(Self, aArg1,{*}aArg2, 1000{ms});     aRes := aMult.Await;     Example.Log('%d * %d = %d', [aArg1, aArg2, aRes]);   finally     aMult.Free;     aPlus2.Free;     aPlus1.Free;   end; end; <\/code><\/pre>\n<p>  <a href=\"https:\/\/drive.google.com\/open?id=0B0QVjOftrvZvR1MwbkE3VWZES0U\">\u041f\u0440\u0438\u043c\u0435\u0440<\/a> \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0434\u043b\u044f Delphi 2007, XE2, XE8.               <\/p>\n<div class=\"clear\"><\/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:\/\/habrahabr.ru\/post\/278985\/\"> https:\/\/habrahabr.ru\/post\/278985\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>       \u0412 C# \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f async\/await. \u0414\u0430\u043b\u0435\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043a\u0430\u043a \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0433\u043e \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043d\u0430 Delphi.<\/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-275876","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/275876","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=275876"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/275876\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=275876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=275876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=275876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}