{"id":182,"date":"2020-11-20T15:37:44","date_gmt":"2020-11-20T07:37:44","guid":{"rendered":"http:\/\/admin.lastdream.net\/?p=182"},"modified":"2020-11-20T15:37:44","modified_gmt":"2020-11-20T07:37:44","slug":"c-awaitzhuanzai","status":"publish","type":"post","link":"https:\/\/admin.vsingers.com\/index.php\/2020\/11\/20\/c-awaitzhuanzai\/","title":{"rendered":"C# Await\uff08\u8f6c\u8f7d\uff09"},"content":{"rendered":"<p>https:\/\/www.cnblogs.com\/haibozhu\/p\/11407647.html<\/p>\n<p>2020.11.18 \u7528\u4e8e\u5ef6\u8fdf\u65f6\u949f\u7b49\u65b9\u9762\u7684\uff0c\u5b58\u5728\u5f02\u6b65\u8c03\u7528\u6ce8\u610f\u4e8b\u9879\uff0c\u5f85\u7ee7\u7eed\u5b66\u4e60\u4e86\u89e3<\/p>\n<p>&nbsp;<\/p>\n<p>\u6bcf\u6b21\u63d0\u5230\u5f02\u6b65\u6211\u90fd\u9009\u62e9\u7ed5\u5f00\uff0c\u611f\u89c9\u6df1\u4e0d\u53ef\u6d4b\uff0c\u6700\u8fd1\u6253\u7b97\u770b\u770b\u5f02\u6b65\uff0c\u4f46\u53c8\u4e0d\u613f\u610f\u770b\u4e66\uff0c\u7f51\u4e0a\u627e\u4e86\u51e0\u4e2a\u89c6\u9891\u770b\uff0c\u53d1\u73b0\u4f20\u667a\u64ad\u5ba2\u7684\u8001\u5e08\u8bb2\u5f02\u6b65\u90fd\u4e0d\u662f\u5f88\u6df1\u5165\uff0c\u5173\u952e\u7684\u95ee\u9898\u4e00\u7b14\u5e26\u8fc7\uff0c\u5012\u662f\u628a\u6211\u5f04\u7cca\u6d82\u4e86\uff0c\u5370\u8c61\u6700\u6df1\u523b\u7684\u662f\u90a3\u4e2a\u8001\u5e08\u8bf4\u7684\u4e00\u53e5\u8bdd\uff1a\u201c\u5728\u5f02\u6b65\u51fd\u6570\u4e2d\uff0cAwait\u4e4b\u540e\u4f1a\u81ea\u52a8\u521b\u5efa\u51fa\u4e00\u4e2a\u7ebf\u7a0b\u201d\u3002\u786e\u5b9e\uff0c\u5728\u63a7\u5236\u53f0\u7a0b\u5e8f\u4e2d\u662f\u8fd9\u6837\uff0c\u4f46\u662f\u5728winform\u4e2d\u5374\u4e0d\u90fd\u5728UI\u7ebf\u7a0b\u4e2d\u6267\u884c\uff0c\u611f\u89c9\u5185\u90e8\u9690\u85cf\u4e0d\u53ef\u544a\u4eba\u7684\u79d8\u5bc6\uff0c\u7d22\u6027\u81ea\u5df1\u63a2\u7d22\u4e00\u4e0b\u3002<\/p>\n<p>\u63a7\u5236\u53f0\u7a0b\u5e8f\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<pre>        static void Main(string[] args)\n        {\n            Console.WriteLine(\"1.\u540c\u6b65ThreaID\uff1a\" + Thread.CurrentThread.ManagedThreadId);\n            RunTimeAsync();\n            Console.WriteLine(\"4.\u5f02\u6b65\u6267\u884c\u5b8c\u6bd5ThreaID\uff1a\" + Thread.CurrentThread.ManagedThreadId);\n            Thread.Sleep(5000);\n            Console.WriteLine(\"5.\u540c\u6b65\u5ef6\u8fdf\u5b8c\u6bd5ThreaID\uff1a\" + Thread.CurrentThread.ManagedThreadId);\n            Console.ReadKey();\n        }\n        public async static Task RunTimeAsync()\n        {\n            Console.WriteLine(\"2.\u8fdb\u5165\u5f02\u6b65ThreaID\uff1a\" + Thread.CurrentThread.ManagedThreadId);\n            await Task.Delay(1000);\n            Console.WriteLine(\"3.\u5f02\u6b65\u6267\u884c\u540eThreaID\uff1a\" + Thread.CurrentThread.ManagedThreadId);\n        }<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<p>\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-183\" src=\"http:\/\/admin.lastdream.net\/wp-content\/uploads\/2020\/11\/965037-20190825121339818-512090053.png\" width=\"278\" height=\"149\" alt=\"\" \/><\/p>\n<p>\u540c\u6837\u7684\u4ee3\u7801\u5728winform\u4e2d\u6267\u884c\uff1a<\/p>\n<p>winform\u7a0b\u5e8f\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<pre>        private void button1_Click(object sender, EventArgs e)\n        {\n            Console.WriteLine(\"1.\u540c\u6b65ThreaID\uff1a\" + Thread.CurrentThread.ManagedThreadId);\n            RunTimeAsync();\n            Console.WriteLine(\"4.\u5f02\u6b65\u6267\u884c\u5b8c\u6bd5ThreaID\uff1a\" + Thread.CurrentThread.ManagedThreadId);\n            Thread.Sleep(2000);\n            Console.WriteLine(\"5.\u540c\u6b65\u5ef6\u8fdf\u5b8c\u6bd5ThreaID\uff1a\" + Thread.CurrentThread.ManagedThreadId);\n\n        }\n        public async static Task RunTimeAsync()\n        {\n            Console.WriteLine(\"2.\u8fdb\u5165\u5f02\u6b65ThreaID\uff1a\" + Thread.CurrentThread.ManagedThreadId);\n            await Task.Delay(1000);\n            Console.WriteLine(\"3.\u5f02\u6b65\u6267\u884c\u540eThreaID\uff1a\" + Thread.CurrentThread.ManagedThreadId);\n        }<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<p>\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-184\" src=\"http:\/\/admin.lastdream.net\/wp-content\/uploads\/2020\/11\/965037-20190825122105537-43793172.png\" width=\"332\" height=\"112\" alt=\"\" \/><\/p>\n<p>\u524d\u540e\u4ee3\u7801\u6267\u884c\u7ed3\u679c\u5bf9\u6bd4\u540e\u53d1\u73b0\uff0c<\/p>\n<p>1.<strong>\u7ebf\u7a0b\u6570\u91cf<\/strong>\uff0cwinfrom\u90fd\u662f\u5728\u4e00\u4e2a\u7ebf\u7a0b\u91cc\u6267\u884c\uff0c\u63a7\u5236\u53f0\u7a0b\u5e8f\u662f\u4e24\u4e2a\u7ebf\u7a0b\u6267\u884c\uff0c\u5176\u4e2d\u5f02\u6b65\u64cd\u4f5c\u5728\u65b0\u7684\u7ebf\u7a0b\u91cc\u3002<\/p>\n<p>2.<strong>\u6267\u884c\u987a\u5e8f<\/strong>\uff0cwinform\u5148\u6267\u884cclick\u4e2d\u540c\u6b65\u64cd\u4f5c\uff0c\u518d\u6267\u884c\u5f02\u6b65\u64cd\u4f5c\uff0c\u63a7\u5236\u53f0\u5374\u76f8\u53cd\u3002<\/p>\n<p>\u6240\u4ee5await\u8fd9\u4e2a\u5173\u952e\u5b57\u5230\u5e95\u5bf9\u5f02\u6b65\u64cd\u4f5c\u52a8\u4e86\u4ec0\u4e48\u624b\u811a\u5462\uff1f<\/p>\n<p>\u5728\u67e5\u4e86\u4e00\u4e9b\u76f8\u5173\u8d44\u6599\u540e\u6709\u4e24\u4e2a\u8bcd\u6bd4\u8f83\u9ad8\u9891\uff1a<span class=\"fontstyle0\">SynchronizationContext \u548cTaskScheduler,<\/span><span class=\"fontstyle0\">\u6267\u884c\u4ee3\u7801\u65f6,Task\u4e0d\u4f1a\u81ea\u5df1\u6267\u884c,\u800c\u662f\u5c06\u6267\u884c\u5185\u5bb9\u653e\u5230SynchronizationContext \u548cTaskScheduler\u6267\u884c\u3002<\/span><\/p>\n<p><span class=\"fontstyle0\">\u5176\u5b9eawait Task.Delay\uff081000\uff09\u6216\u8005await AsyncFun()\uff08\u5f02\u6b65\u64cd\u4f5c\uff09\u7b49\u4ef7\u4e8e\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/span><\/p>\n<p>Task t = AsyncFun();<br \/>\nvar currentContext = SynchronizationContext.Current;<br \/>\nif (null\u00a0 == currentContext)<br \/>\n{<br \/>\nt.ContinueWith((te) =&gt; { Run(); }, TaskScheduler.Current);<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nt.ContinueWith((te) =&gt; { Run(); }, TaskScheduler.FromCurrentSynchronizationContext())<\/p>\n<p>}<\/p>\n<p>\u5982\u679cSynchronizationContext\u4e3anull\u5c31\u5728TaskScheduler\u4e2d\u6267\u884c\uff0c\u5982\u679c\u4e0d\u4e3anull\u5219\u5728FromCurrentSynchronizationContext\u4e2d\u6267\u884c\u3002<\/p>\n<p>\u5148\u4e86\u89e3\u4e00\u4e0bTaskScheduler&#8212;&#8212;&#8211;CSDN\u4e2d\u6ce8\u89e3\u662f\uff1a\u8868\u793a\u4e00\u4e2a\u5904\u7406\u5c06\u4efb\u52a1\u6392\u961f\u5230\u7ebf\u7a0b\u4e2d\u7684\u4f4e\u7ea7\u5de5\u4f5c\u7684\u5bf9\u8c61\u3002<\/p>\n<p>\u770b\u4e00\u4e0bTaskScheduler\u7684\u5143\u6570\u636e\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-185\" src=\"http:\/\/admin.lastdream.net\/wp-content\/uploads\/2020\/11\/965037-20190825130053999-639735266.png\" width=\"1280\" height=\"588\" alt=\"\" \/><\/p>\n<p>TaskScheduler\u5176\u5b9e\u5c31\u662f\u5c06Task\u653e\u5230Threadpool\u6267\u884c\uff0c\u901a\u8fc7\u5165\u961f\u51fa\u961f\u7684\u65b9\u5f0f\u6392\u5217\u6267\u884c\u987a\u5e8f\u3002<\/p>\n<p>\u6839\u636e\u7b49\u4ef7\u4ee3\u7801\u6211\u4eec\u8981\u533a\u5206\u4e00\u4e0b\u63a7\u5236\u53f0\u7a0b\u5e8f\u4e0ewinfrom\u7a0b\u5e8f\u5f02\u6b65\u64cd\u4f5c\u7684\u6267\u884c\u65b9\u5f0f\uff0c<\/p>\n<p>\u6211\u4eec\u6267\u884c\u8fd9\u6761\u4ee3\u7801\uff1avar currentContext = SynchronizationContext.Current;<\/p>\n<p>\u5728\u63a7\u5236\u53f0\u7a0b\u5e8f\u4e2dcurrentContext \u4e3anull<\/p>\n<p>\u5728winform\u4e2dcurrentContext \u4e3aSystem.Windows.Forms.WindowsFormsSynchronizationContext<\/p>\n<p>\u90a3\u4e48\u63a7\u5236\u53f0\u7a0b\u5e8f\u662f\u5728TaskScheduler.Current\u6240\u5728\u7684\u7ebf\u7a0b\u6c60\u6267\u884c\uff0c\u6211\u4eec\u770b\u5230\u63a7\u5236\u53f0\u7684await \u5f02\u6b65\u64cd\u4f5c\u5728\u5176\u4ed6\u7ebf\u7a0b\u4e2d\u6267\u884c\u4e00\u81f4\u3002<\/p>\n<p>\u4e0b\u9762\u5206\u6790\u4e00\u4e0bwinform\u7684\u6267\u884c\u65b9\u5f0f\uff1a<\/p>\n<p>WindowsFormsSynchronizationContext\u7ee7\u627f\u81eaSynchronizationContext\uff0c<\/p>\n<p>https:\/\/www.cnblogs.com\/lzxianren\/p\/SynchronizationContext.html\u00a0 \u00a0\u53c2\u8003\u4e86\u8fd9\u7bc7\u6587\u7ae0<\/p>\n<p>\u91cc\u9762\u6709\u4e24\u70b9\uff1a<\/p>\n<p>public virtual void Send(SendOrPostCallback d, Object state)<\/p>\n<p>{<\/p>\n<p>d(state);<\/p>\n<p>}<\/p>\n<p>public virtual void Post(SendOrPostCallback d, Object state)<\/p>\n<p>{<\/p>\n<p>ThreadPool.QueueUserWorkItem(new WaitCallback(d), state);<\/p>\n<p>}<\/p>\n<p>\u5728UI\u7ebf\u7a0b\u6267\u884c\u662f\uff0c\u4f1a\u5c06\u6267\u884c\u7684\u4efb\u52a1\u7528Send\u65b9\u6cd5\u653e\u5230\u6d88\u606f\u6cf5\u4e2d\uff0c\u5f53\u65f6UI\u4e2d\u540c\u6b65\u6267\u884c\u7684\u64cd\u4f5c\u6267\u884c\u5b8c\u6bd5\uff0c\u518d\u6267\u884c\u8be6\u7ec6\u6cf5\u4e2d\u7684\u6570\u636e\uff0c\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48UI\u4e2d\u53ea\u5728\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u6267\u884c\uff0c\u800c\u4e14\u603b\u662f\u5728\u6700\u540e\u6267\u884c\u3002<\/p>\n<p>\u603b\u7ed3\uff1a\u7528 await \u8bed\u53e5\u7b49\u5f85\u4e00\u4e2a\u4efb\u52a1\u5b8c\u6210\uff0c\u5f53\u8be5\u65b9\u6cd5\u5728 await \u5904\u6682\u505c\u65f6\uff0c\u5c31\u53ef\u4ee5\u6355\u6349\u4e0a\u4e0b\u6587\uff08context\uff09\u3002\u5982\u679c\u5f53\u524d SynchronizationContext \u4e0d\u4e3a\u7a7a\uff0c\u8fd9\u4e2a\u4e0a\u4e0b\u6587\u5c31\u662f\u5f53\u524dSynchronizationContext\uff1b\u5982\u679c\u5f53\u524d SynchronizationContext \u4e3a\u7a7a\uff0c\u5219\u8fd9\u4e2a\u4e0a\u4e0b\u6587\u4e3a\u5f53\u524daskScheduler\uff0c\u8be5\u65b9\u6cd5\u4f1a\u5728\u8fd9\u4e2a\u4e0a\u4e0b\u6587\u4e2d\u7ee7\u7eed\u8fd0\u884c\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u8fd0\u884c UI \u7ebf\u7a0b\u65f6\u91c7\u7528 UI \u4e0a\u4e0b\u6587\uff0c\u5904\u7406 ASP.NET \u8bf7\u6c42\u65f6\u91c7\u7528 ASP.NET \u8bf7\u6c42\u4e0a\u4e0b\u6587\uff0c\u5176\u4ed6\u5f88\u591a\u60c5\u51b5\u4e0b\u5219\u91c7\u7528\u7ebf\u7a0b\u6c60\u4e0a\u4e0b\u6587\u3002\u56e0\u6b64\uff0c\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6bcf\u4e2a\u540c\u6b65\u7a0b\u5e8f\u5757\u4f1a\u8bd5\u56fe\u5728\u539f\u59cb\u7684\u4e0a\u4e0b\u6587\u4e2d\u6062\u590d\u8fd0\u884c\u3002\u5982\u679c\u5728 UI\u7ebf\u7a0b\u4e2d\u8c03\u7528 \uff0c\u8fd9\u4e2a\u65b9\u6cd5\u7684\u6bcf\u4e2a\u540c\u6b65\u7a0b\u5e8f\u5757\u90fd\u5c06\u5728\u6b64 UI \u7ebf\u7a0b\u4e0a\u8fd0\u884c\u3002\u4f46\u662f\uff0c\u5982\u679c\u5728\u7ebf\u7a0b\u6c60\u7ebf\u7a0b\u4e2d\u8c03\u7528\uff0c\u6bcf\u4e2a\u540c\u6b65\u7a0b\u5e8f\u5757\u5c06\u5728\u7ebf\u7a0b\u6c60\u7ebf\u7a0b\u4e0a\u8fd0\u884c\u3002<\/p>\n<p>\u63a7\u5236\u53f0\u7a0b\u5e8f\u4ee5\u53ca\u7c7b\u5e93\u662f\u5728TaskScheduler\u4e2d\u8c03\u5ea6\u6267\u884c\uff0cUI\u7a0b\u5e8f\u5219\u662f\u5728SynchronizationContext\u8c03\u5ea6\u6267\u884c\u3002<\/p>\n<p><em id=\"__mceDel\"><span class=\"fontstyle0\"><span class=\"fontstyle2\"><span class=\"fontstyle3\"><span class=\"fontstyle4\">\u53c2\u8003\u4e86\uff1ahttps:\/\/blog.csdn.net\/wlk1229\/article\/details\/81287374\u00a0 \u00a0<\/span><\/span><\/span><\/span><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/www.cnblogs.com\/haibozhu\/p\/11407647.html 2020.1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":183,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-182","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c_net"],"_links":{"self":[{"href":"https:\/\/admin.vsingers.com\/index.php\/wp-json\/wp\/v2\/posts\/182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/admin.vsingers.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/admin.vsingers.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/admin.vsingers.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/admin.vsingers.com\/index.php\/wp-json\/wp\/v2\/comments?post=182"}],"version-history":[{"count":0,"href":"https:\/\/admin.vsingers.com\/index.php\/wp-json\/wp\/v2\/posts\/182\/revisions"}],"wp:attachment":[{"href":"https:\/\/admin.vsingers.com\/index.php\/wp-json\/wp\/v2\/media?parent=182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/admin.vsingers.com\/index.php\/wp-json\/wp\/v2\/categories?post=182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/admin.vsingers.com\/index.php\/wp-json\/wp\/v2\/tags?post=182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}