{"id":5544,"date":"2022-07-16T18:38:26","date_gmt":"2022-07-16T10:38:26","guid":{"rendered":"http:\/\/blog.qdac.cc\/?p=5544"},"modified":"2022-07-22T21:07:46","modified_gmt":"2022-07-22T13:07:46","slug":"%e6%95%99%e7%a8%8bizasyncinvoker-%e5%bc%82%e6%ad%a5%e7%bc%96%e7%a8%8b%e4%b9%8b%e5%9b%9b%ef%bc%9aasync-await","status":"publish","type":"post","link":"https:\/\/blog.qdac.cc\/?p=5544","title":{"rendered":"[\u6559\u7a0b]ZAsync \u5f02\u6b65\u7f16\u7a0b\u4e4b\u516d\uff1a\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u4f7f\u7528async\/await"},"content":{"rendered":"\n<p>async\/await \u662f\u4e00\u79cd\u534f\u540c\u5f0f\u7684\u5f02\u6b65\u6267\u884c\u673a\u5236\uff0c\u5b83\u8981\u6c42\u957f\u65f6\u95f4\u6267\u884c\u7684\u4f5c\u4e1a\uff0c\u4e3b\u52a8\u53bb\u91ca\u653e\u5f53\u524d\u7ebf\u7a0b\u7684\u63a7\u5236\u6743\uff0c\u4ece\u800c\u8ba9\u5176\u5b83\u540c\u4e00\u7ebf\u7a0b\u7684\u51fd\u6570\u83b7\u5f97\u6267\u884c\u673a\u4f1a\uff0c\u4ece\u800c\u5145\u5206\u5229\u7528CPU\u8d44\u6e90\u3002<\/p>\n\n\n\n<p>\u8981\u5728\u4f60\u7684\u7a0b\u5e8f\u4e2d\u652f\u6301 async\/await \u6a21\u5f0f\uff0c\u9700\u8981 \uff1a<\/p>\n\n\n\n<p>1\u3001\u5f15\u7528  zbar.async \u548c vcl.zbar.async\/fmx.zbar.async \u5355\u5143\uff08\u8fd9\u4e24\u4e2a\u5355\u5143\u53d6\u51b3\u4e8e\u4f60\u5177\u4f53\u4f7f\u7528\u7684\u6846\u67b6\uff0c\u4e3b\u8981\u7528\u4e8e\u652f\u6301\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u4f7f\u7528 async\/await\uff09\u3002<\/p>\n\n\n\n<p>2\u3001\u6267\u884cTZAsync.Async(\u533f\u540d\u51fd\u6570,[\u9644\u52a0\u53c2\u6570\u5217\u8868]) \u5373\u53ef\u542f\u52a8\u4e00\u4e2a\u5f53\u524d\u7ebf\u7a0b\u7684\u5f02\u6b65\u8c03\u7528\uff0c\u5f53\u9047\u5230 await \u6216\u8005\u7528\u6237\u8c03\u7528 TZAsync.Yield \u65f6\uff0c\u5f02\u6b65\u8c03\u7528\u4f1a\u88ab\u89e6\u53d1\u6267\u884c\u3002\u4e3b\u7ebf\u7a0b\u4e2d\u7684 async \u8c03\u7528\u4f1a TZAsync \u5728\u6d88\u606f\u5faa\u73af\u7a7a\u95f2\u65f6\u81ea\u52a8\u8c03\u7528\u4e86 TZAsync.Yield\uff0c\u6240\u4ee5\u53ef\u4ee5\u4e0d\u9700\u8981\u8c03\u7528\uff0c\u4f46\u5728\u540e\u53f0\u7ebf\u7a0b\u4e2d\uff0c\u56e0\u4e3a TThread \u672c\u8eab\u4f5c\u8005\u5e76\u6ca1\u6709\u627e\u5230\u76f4\u63a5\u6ce8\u5165\u7684\u5730\u65b9\uff0c\u6240\u4ee5\u9700\u8981\u7528\u6237\u624b\u52a8\u8c03\u7528 Yield \u6765\u89e6\u53d1\u3002<\/p>\n\n\n\n<p>3\u3001\u6267\u884cTAsync.Await(\u533f\u540d\u51fd\u6570,[\u9644\u52a0\u53c2\u6570\u5217\u8868]) \u5373\u53ef\u542f\u52a8\u4e00\u4e2a\u5f53\u524d\u7ebf\u7a0b\u7684\u5f02\u6b65\u8c03\u7528\uff0c\u5e76\u4e14\u7b49\u5f85\u5b83\u5b8c\u6210\u3002<\/p>\n\n\n\n<p>\u5728\u56de\u8c03\u51fd\u6570\u4e2d\uff0c\u4f7f\u7528 IZAsyncInvoker \u6216 TZAsync \u7684 Yield \u53ef\u4ee5\u8ba9\u51fa\u5f53\u524d\u51fd\u6570\u6267\u884c\u6743\u5230\u4e0b\u4e00\u4e2a\u51fd\u6570\u3002\u8bf7\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>procedure TForm2.Button1Click(Sender: TObject);\nbegin\n  TZAsync.Async(\n    procedure(AInvoker: IZAsyncInvoker)\n    begin\n      ShowMessage('Hello,async first');\n      AInvoker.Yield;\n      ShowMessage('Hello,async second');\n    end);\n  ShowMessage('Hello,first');\n  TZAsync.Await(\n    procedure(AInvoker: IZAsyncInvoker)\n    begin\n      ShowMessage('Hello,async third');\n    end);\n  ShowMessage('Hello,final');\nend;\n<\/code><\/pre>\n\n\n\n<p>\u8fd0\u884c\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u5b9e\u9645\u5448\u73b0\u63d0\u793a\u7684\u987a\u5e8f\u662f\uff08\u622a\u56fe\u4e3a\u8001\u7248\u672c\uff0c\u65b0\u7248\u672c\u52a0\u4e86 TZAsync \u524d\u7f00\uff09\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Hello,first<\/li><li>Hello,async first<\/li><li>Hello,async third<\/li><li>Hello,async second<\/li><li>Hello,final<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/blog.qdac.cc\/wp-content\/uploads\/2022\/07\/await-1.gif\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/blog.qdac.cc\/wp-content\/uploads\/2022\/07\/await-1-1024x640.gif\" alt=\"\" class=\"wp-image-5548\" srcset=\"https:\/\/blog.qdac.cc\/wp-content\/uploads\/2022\/07\/await-1-1024x640.gif 1024w, https:\/\/blog.qdac.cc\/wp-content\/uploads\/2022\/07\/await-1-300x187.gif 300w, https:\/\/blog.qdac.cc\/wp-content\/uploads\/2022\/07\/await-1-768x480.gif 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption>async\/await \u6548\u679c\u6f14\u793a<\/figcaption><\/figure>\n\n\n\n<p>\u8fbe\u5230\u4e86\u6211\u4eec\u9884\u671f\u7684 async\/await \u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>async\/await \u662f\u4e00\u79cd\u534f\u540c\u5f0f\u7684\u5f02 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[8],"tags":[],"class_list":["post-5544","post","type-post","status-publish","format-standard","hentry","category-delphi"],"views":1710,"_links":{"self":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/5544","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5544"}],"version-history":[{"count":7,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/5544\/revisions"}],"predecessor-version":[{"id":5574,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/5544\/revisions\/5574"}],"wp:attachment":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}