{"id":5454,"date":"2022-03-21T10:13:24","date_gmt":"2022-03-21T02:13:24","guid":{"rendered":"http:\/\/blog.qdac.cc\/?p=5454"},"modified":"2022-03-21T10:13:25","modified_gmt":"2022-03-21T02:13:25","slug":"%e6%95%99%e7%a8%8b-delphi-%e8%87%aa%e5%b8%a6%e5%bc%82%e6%ad%a5%e6%a8%a1%e5%9e%8b%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/blog.qdac.cc\/?p=5454","title":{"rendered":"[\u6559\u7a0b] Delphi \u81ea\u5e26\u5f02\u6b65\u6a21\u578b\u8be6\u89e3"},"content":{"rendered":"\n<p>Delphi \u5728\u65b0\u7248\u4e2d\u5f15\u5165\u4e86\u4e00\u4e2a\u65b0\u7684\u5f02\u6b65\u7f16\u7a0b\u6a21\u578b\uff0c\u4e00\u4e2a\u65b0\u7684\u63a5\u53e3 IAsyncResult \u88ab\u5305\u542b\u5728 System.Types \u91cc\u3002\u6211\u4eec\u5148\u6765\u5927\u6982\u8bf4\u4e0b Delphi \u81ea\u5df1\u8fd9\u4e2a\u5f02\u6b65\u7f16\u7a0b\u6a21\u578b\u7684\u601d\u8def\uff1a<\/p>\n\n\n\n<p>1\u3001\u5f02\u6b65\u7f16\u7a0b\u7684\u51fd\u6570\u540d\uff0c\u5b98\u65b9\u662f\u4ee5 BeginXXX\u6253\u5934\uff0c\u6bd4\u5982 BeginReceive\u3001BeginWrite \u7b49\u7b49 \uff0c\u5b83\u4eec\u8d1f\u8d23\u5f00\u59cb\u4e00\u4e2a\u5f02\u6b65\u8c03\u7528\uff0c\u5e76\u4e14\u8fd4\u56de\u4e00\u4e2a IAsyncResult \u7c7b\u578b\u7684\u63a5\u53e3\uff0c\u65b9\u4fbf\u4e0a\u5c42\u63a7\u5236\u5f02\u6b65\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<p>2\u3001\u5f00\u542f\u7684\u5f02\u6b65\u8c03\u7528\u8fc7\u7a0b\uff0c\u5b8c\u6210\u4e86\u4f1a\u8c03\u7528\u4f60\u63d0\u4f9b\u7684 TAsyncCallback\/TAsyncCallbackEvent \u5f02\u6b65\u56de\u8c03\u51fd\u6570\u3002\u5982\u679c\u4e0d\u9700\u8981\uff0c\u53ef\u4ee5\u4e0d\u63d0\u4f9b\u3002<\/p>\n\n\n\n<p>3\u3001\u53ef\u4ee5\u4f7f\u7528 EndXXX \u6765\u5f3a\u5236\u7ed3\u675f\u4e00\u4e2a\u5f02\u6b65\u8c03\u7528\u8fc7\u7a0b\uff0c\u5982\u679c EndReceive\u3001EndWrite \u7b49\u7b49\u3002EndXXX \u7cfb\u5217\u51fd\u6570\u4f1a\u7b49\u5f85\u5f02\u6b65\u8c03\u7528\u5b8c\u6210\uff0c\u7136\u540e\u624d\u8fd4\u56de\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4e00\u4e2a\u5f02\u6b65\u8c03\u7528\u5f00\u542f\uff0c\u8981\u7b49\u5f85\u5b83\u5b8c\u6210\u7684\u8bdd\uff0c\u53ef\u4ee5\u8c03\u7528 EndXXX \u6765\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>4\u3001\u5f00\u542f\u7684\u5f02\u6b65\u8c03\u7528\u8fc7\u7a0b\uff0c\u53ef\u4ee5\u4f7f\u7528 IAsyncResult.Cancel  \u6765\u53d6\u6d88\u8c03\u7528\uff0c\u4f46\u662f\u8981\u6ce8\u610f\u4e0d\u662f\u6240\u6709\u7684\u5f02\u6b65\u64cd\u4f5c\u90fd\u652f\u6301\u53d6\u6d88\u7684\uff0c\u6240\u4ee5\u8fd9\u4e2a\u63a5\u53e3\u53ef\u80fd\u8fd4\u56de false\u3002\u6240\u4ee5\uff0c\u5982\u679c\u4e00\u4e2a\u5f02\u6b65\u8c03\u7528\u8fc7\u7a0b\u53d6\u6d88\u5931\u8d25\u7684\u8bdd\uff0c\u8c03\u7528 EndXXX \u6765\u7b49\u5f85\u7ed3\u675f\u9000\u51fa\u624d\u662f\u5b89\u5168\u7684\uff0c\u5426\u5219\u53ef\u80fd\u4f1a\u6709\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u55ef\uff0c\u57fa\u672c\u7684\u6d41\u7a0b\u5c31\u662f\u8fd9\u4e2a\u6837\u5b50\uff1aStarter.BeginXXX->\u56de\u8c03\u6216Starter.EndXXX\u6216IAsyncResult.Cancel\u3002<\/p>\n\n\n\n<p>\u4ece\u4e0a\u9762\u7684\u6d41\u7a0b\u5c31\u53ef\u4ee5\u5206\u914d\u51fa\uff0cDelphi \u81ea\u5e26\u7684\u5f02\u6b65\u7f16\u7a0b\u6a21\u578b\u6709\u4e24\u90e8\u5206\u7ec4\u6210\uff1a<\/p>\n\n\n\n<p>1\u3001\u670d\u52a1\u7aef\u63d0\u4f9b\u5f02\u6b65\u8c03\u7528\u652f\u6301\u7684\u5b9e\u73b0\uff0c\u5b83\u8981\u5b9e\u73b0\u652f\u6301 IAsyncResult \u63a5\u53e3\u7684\u652f\u6301\uff0c\u5e76\u63d0\u4f9b BeginXXX\/EndXXX \u7cfb\u5217\u51fd\u6570\u3002<\/p>\n\n\n\n<p>2\u3001\u8c03\u7528\u7aef\u6309\u4e0a\u9762\u8bf4\u7684\u5b8c\u6210\u6807\u51c6\u7684\u8c03\u7528\u8fc7\u7a0b\u3002BeginXXX \u8fd4\u56de\u7684\u7ed3\u679c\u5b9e\u4f8b\u4e00\u822c\u60c5\u51b5\u4e0b\u9700\u8981\u4fdd\u5b58\uff0c\u4ee5\u4fbf\u5728\u5bf9\u8c61\u91ca\u653e\u4e4b\u524d\uff0c\u4e2d\u6b62\u5f02\u6b65\u64cd\u4f5c\uff0c\u907f\u514d\u8bbf\u95ee\u8fdd\u4f8b\u3002\u9664\u975e\u4f60\u786e\u8ba4\u5f02\u6b65\u8c03\u7528\u7684\u5185\u90e8\u64cd\u4f5c\u4e0e\u5f53\u524d\u8c03\u7528\u7684\u5b9e\u4f8b\u6ca1\u6709\u5173\u7cfb\uff0c\u5426\u5219\u5b89\u5168\u8d77\u89c1\uff0c\u8fd8\u662f\u8981\u614e\u91cd\u3002<\/p>\n\n\n\n<p>IAsyncResult \u7684\u5b9e\u73b0\u53ef\u4ee5\u7ee7\u627f\u81ea TBaseAsyncResult\uff0c\u5b83\u4f4d\u4e8e System.Classes \u5355\u5143\u3002\u5ba2\u6237\u7aef\u5982\u679c\u8981\u5728\u4e2d\u95f4\u5224\u65ad\u6267\u884c\u7684\u7ed3\u679c\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u63a5\u53e3\u6765\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>AsyncContext \u662f\u7528\u6237 BeginXXX \u4f20\u5165\u7684\u5173\u8054\u5bf9\u8c61\u5b9e\u4f8b<\/li><li>AsyncWaitEvent \u662f\u5185\u90e8\u7528\u6765\u7b49\u5f85\u4f5c\u4e1a\u5b8c\u6210\u7684\u4e8b\u4ef6\u5b9e\u4f8b\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u5b83\u7b49\u5f85\u591a\u4e2a\u4e8b\u4ef6\u6216\u5176\u4e2d\u4e00\u4e2a\u5b8c\u4e8b<\/li><li>CompletedSynchronously \u7528\u6765\u6807\u5fd7\u662f\u5426\u5f02\u6b65\u8c03\u7528\u80fd\u540c\u6b65\u5b8c\u6210\uff0c\u6362\u53e5\u8bdd\u8bf4\uff0c\u5177\u4f53\u7684\u8c03\u7528\u53ef\u80fd\u8fd4\u56de\u524d\u5df2\u7ecf\u5b8c\u6210<\/li><li>IsComplete \u7528\u6765\u6807\u5fd7\u5f02\u6b65\u8c03\u7528\u662f\u5426\u5b8c\u6210<\/li><li>IsCancel \u7528\u6765\u6807\u5fd7\u5f02\u6b65\u8c03\u7528\u662f\u5426\u88ab\u53d6\u6d88<\/li><\/ul>\n\n\n\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u53ef\u4ee5\u770b\u5230\u5f02\u6b65\u7f16\u7a0b\u8fd8\u662f\u9700\u8981\u6bd4\u8f83\u591a\u7684\u8bf4\u9053\u7684\u3002<\/p>\n\n\n\n<p>\u6b64\u884c\u4ee5\u4e0b\u65e0\u6b63\u6587\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Delphi \u5728\u65b0\u7248\u4e2d\u5f15\u5165\u4e86\u4e00\u4e2a\u65b0\u7684\u5f02\u6b65 [&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,603],"tags":[],"class_list":["post-5454","post","type-post","status-publish","format-standard","hentry","category-delphi","category-603"],"views":3494,"_links":{"self":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/5454","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=5454"}],"version-history":[{"count":1,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/5454\/revisions"}],"predecessor-version":[{"id":5456,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/5454\/revisions\/5456"}],"wp:attachment":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5454"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5454"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5454"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}