{"id":19,"date":"2021-08-01T11:15:34","date_gmt":"2021-08-01T11:15:34","guid":{"rendered":"https:\/\/www.miie.net\/?p=19"},"modified":"2021-08-01T11:15:34","modified_gmt":"2021-08-01T11:15:34","slug":"csharp-synchronous","status":"publish","type":"post","link":"https:\/\/diji.net\/?p=19","title":{"rendered":"C# \u5f02\u6b65"},"content":{"rendered":"<p>\u5f02\u6b65\u51fd\u6570\u7b80\u4ecb<br \/>\n\u4e00\u822c\u6307 async \u4fee\u9970\u7b26\u58f0\u660e\u5f97\u3001\u53ef\u5305\u542bawait\u8868\u8fbe\u5f0f\u5f97\u65b9\u6cd5\u6216\u533f\u540d\u51fd\u6570\u3002<\/p>\n<p>\u58f0\u660e\u65b9\u5f0f<br \/>\n\u5f02\u6b65\u65b9\u6cd5\u7684\u58f0\u660e\u8bed\u6cd5\u4e0e\u5176\u4ed6\u65b9\u6cd5\u5b8c\u5168\u4e00\u6837, \u53ea\u662f\u9700\u8981\u5305\u542b async \u5173\u952e\u5b57\u3002async\u53ef\u4ee5\u51fa\u73b0\u5728\u8fd4\u56de\u503c\u4e4b\u524d\u7684\u4efb\u4f55\u4f4d\u7f6e, \u5982\u4e0b\u793a\u4f8b:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n    async public static void GetInfoAsync()\n        {\n           \/\/...\n        }\n\n    public async static void GetInfoAsync()\n        {\n           \/\/...\n        }\n\n    public static async void GetInfoAsync()\n        {\n            \/\/...\n        }\n<\/pre>\n<p>\u5f02\u6b65\u65b9\u6cd5\u7684\u8fd4\u56de\u7c7b\u578b<br \/>\n\u5f02\u6b65\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u53ea\u80fd\u4e3a: void\u3001Task\u3001Task<TResult>\u3002<br \/>\nTask<TResult>: \u4ee3\u8868\u4e00\u4e2a\u8fd4\u56de\u503cT\u7c7b\u578b\u7684\u64cd\u4f5c\u3002<br \/>\nTask: \u4ee3\u8868\u4e00\u4e2a\u65e0\u8fd4\u56de\u503c\u7684\u64cd\u4f5c\u3002<br \/>\nvoid: \u4e3a\u4e86\u548c\u4f20\u7edf\u7684\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u517c\u5bb9\u800c\u8bbe\u8ba1\u3002<\/p>\n<p>await(\u7b49\u5f85)<br \/>\nawait\u7b49\u5f85\u7684\u662f\u4ec0\u4e48? \u53ef\u4ee5\u662f\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c(Task)\u3001\u4ea6\u6216\u8005\u662f\u5177\u5907\u8fd4\u56de\u503c\u7684\u5f02\u6b65\u64cd\u4f5c(Task<TResult>)\u7684\u503c, \u5982\u4e0b:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n    public async static void GetInfoAsync()\n        {\n            await GetData(); \/\/ \u7b49\u5f85\u5f02\u6b65\u64cd\u4f5c, \u65e0\u8fd4\u56de\u503c\n            await GetData&lt;int&gt;(1); \/\/\u7b49\u5f85\u5f02\u6b65\u64cd\u4f5c, \u8fd4\u56de\u503c int\n        }\n\n    static Task GetData()\n        {\n            \/\/...\n            return null;\n        }\n\n    static Task&lt;T&gt; GetData&lt;T&gt;(int a)\n        {\n            \/\/...\n            return null;\n        }\n<\/pre>\n<p>\u6ce8: await \u6700\u7ec8\u64cd\u4f5c\u7684\u662f\u4e00\u4e2a\u503c, \u5f53\u7136, \u4e5f\u53ef\u4ee5\u662f\u65e0\u503c,  \u5982\u4e0aGetData() , \u5426\u5219\u5c31\u662f\u4e00\u4e2a Task<T>  \u5982\u4e0a:  GetData<T>()<\/p>\n<h2>\u539f\u6587\u94fe\u63a5\uff1a<a href=\"https:\/\/www.cnblogs.com\/zh7791\/p\/9951478.html\">https:\/\/www.cnblogs.com\/zh7791\/p\/9951478.html<\/a><\/h2>\n<p>async\u7528\u6765\u4fee\u9970\u65b9\u6cd5\uff0c\u8868\u660e\u8fd9\u4e2a\u65b9\u6cd5\u662f\u5f02\u6b65\u7684\uff0c\u58f0\u660e\u7684\u65b9\u6cd5\u7684\u8fd4\u56de\u7c7b\u578b\u5fc5\u987b\u4e3a\uff1avoid\u6216Task\u6216Task<TResult>\u3002\u8fd4\u56de\u7c7b\u578b\u4e3aTask\u7684\u5f02\u6b65\u65b9\u6cd5\u4e2d\u65e0\u9700\u4f7f\u7528return\u8fd4\u56de\u503c\uff0c\u800c\u8fd4\u56de\u7c7b\u578b\u4e3aTask<TResult>\u7684\u5f02\u6b65\u65b9\u6cd5\u4e2d\u5fc5\u987b\u4f7f\u7528return\u8fd4\u56de\u4e00\u4e2aTResult\u7684\u503c<br \/>\nawait\u5fc5\u987b\u7528\u6765\u4fee\u9970Task\u6216Task<TResult>\uff0c\u800c\u4e14\u53ea\u80fd\u51fa\u73b0\u5728\u5df2\u7ecf\u7528async\u5173\u952e\u5b57\u4fee\u9970\u7684\u5f02\u6b65\u65b9\u6cd5\u4e2d\u3002<br \/>\nasync\/await\u5fc5\u987b\u6210\u5bf9\u51fa\u73b0\u624d\u6709\u610f\u4e49\uff0c\u5047\u5982\u4e00\u4e2a\u65b9\u6cd5\u58f0\u660e\u4e3aasync\uff0c\u4f46\u5374\u6ca1\u6709\u4f7f\u7528await\u5173\u952e\u5b57\uff0c\u5219\u8fd9\u4e2a\u65b9\u6cd5\u5728\u6267\u884c\u7684\u65f6\u5019\u5c31\u88ab\u5f53\u4f5c\u540c\u6b65\u65b9\u6cd5\uff0c\u8fd9\u65f6\u7f16\u8bd1\u5668\u4e5f\u4f1a\u629b\u51fa\u8b66\u544a\u63d0\u793aasync\u4fee\u9970\u7684\u65b9\u6cd5\u4e2d\u6ca1\u6709\u4f7f\u7528await\uff0c\u5c06\u88ab\u4f5c\u4e3a\u540c\u6b65\u65b9\u6cd5\u4f7f\u7528\u3002<br \/>\nasync\u5173\u952e\u5b57\u8868\u660e\u53ef\u4ee5\u5728\u65b9\u6cd5\u5185\u90e8\u4f7f\u7528await\u5173\u952e\u5b57\uff0c\u65b9\u6cd5\u5728\u6267\u884c\u5230await\u524d\u90fd\u662f\u540c\u6b65\u6267\u884c\u7684\uff0c\u8fd0\u884c\u5230await\u5904\u5c31\u4f1a\u6302\u8d77\uff0c\u5e76\u8fd4\u56de\u5230Main\u65b9\u6cd5\u4e2d\uff0c\u76f4\u5230await\u6807\u8bb0\u7684Task\u6267\u884c\u5b8c\u6bd5\uff0c\u624d\u5524\u9192\u56de\u5230await\u70b9\u4e0a\uff0c\u7ee7\u7eed\u5411\u4e0b\u6267\u884c\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nstatic void Main(string&#x5B;] args)\n{\n    Console.WriteLine(&quot;\u5f00\u59cb\u83b7\u53d6\u535a\u5ba2\u56ed\u9996\u9875\u5b57\u7b26\u6570\u91cf&quot;);\n    Task&lt;int&gt; task1 = CountCharsAsync(&quot;http:\/\/www.cnblogs.com&quot;);\n    Console.WriteLine(&quot;\u5f00\u59cb\u83b7\u53d6\u767e\u5ea6\u9996\u9875\u5b57\u7b26\u6570\u91cf&quot;);\n    Task&lt;int&gt; task2 = CountCharsAsync(&quot;http:\/\/www.baidu.com&quot;);\n \n    Console.WriteLine(&quot;Main\u65b9\u6cd5\u4e2d\u505a\u5176\u4ed6\u4e8b\u60c5&quot;);\n \n    Console.WriteLine(&quot;\u535a\u5ba2\u56ed:&quot; + task1.Result);\n    Console.WriteLine(&quot;\u767e\u5ea6:&quot; + task2.Result);\n}\n \nstatic async Task&lt;int&gt; CountCharsAsync(string url)\n{\n    WebClient wc = new WebClient();\n    string result = await wc.DownloadStringTaskAsync(new Uri(url));\n    return result.Length;\n}\n<\/pre>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nstatic void Main(string&#x5B;] args)\n{\n    Console.WriteLine(&quot;-------\u4e3b\u7ebf\u7a0b\u542f\u52a8-------&quot;);\n    Task&lt;int&gt; task = GetLengthAsync();\n    Console.WriteLine(&quot;Main\u65b9\u6cd5\u505a\u5176\u4ed6\u4e8b\u60c5&quot;);\n    Console.WriteLine(&quot;Task\u8fd4\u56de\u7684\u503c&quot; + task.Result);\n    Console.WriteLine(&quot;-------\u4e3b\u7ebf\u7a0b\u7ed3\u675f-------&quot;);\n}\n \nstatic async Task&lt;int&gt; GetLengthAsync()\n{\n    Console.WriteLine(&quot;GetLengthAsync Start&quot;);\n    string str = await GetStringAsync();\n    Console.WriteLine(&quot;GetLengthAsync End&quot;);\n    return str.Length;\n}\n \nstatic Task&lt;string&gt; GetStringAsync()\n{\n    return Task&lt;string&gt;.Run(() =&gt; { Thread.Sleep(2000); return &quot;finished&quot;; });\n}\n<\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f02\u6b65\u51fd\u6570\u7b80\u4ecb \u4e00\u822c\u6307 async \u4fee\u9970\u7b26\u58f0\u660e\u5f97\u3001\u53ef\u5305\u542bawait\u8868\u8fbe\u5f0f\u5f97\u65b9\u6cd5\u6216\u533f\u540d\u51fd\u6570\u3002 \u58f0\u660e\u65b9\u5f0f \u5f02\u6b65\u65b9\u6cd5\u7684\u58f0\u660e\u8bed\u6cd5\u4e0e\u5176\u4ed6\u65b9\u6cd5\u5b8c\u5168\u4e00\u6837, \u53ea\u662f\u9700\u8981\u5305\u542b async \u5173\u952e\u5b57\u3002async\u53ef\u4ee5\u51fa\u73b0\u5728\u8fd4\u56de\u503c\u4e4b\u524d\u7684\u4efb\u4f55\u4f4d\u7f6e, \u5982\u4e0b\u793a\u4f8b: async public static void GetInfoAsync() { \/\/&#8230; } public async static void GetInfoAsync() { \/\/&#8230; } public static async void GetInfoAsync() { \/\/&#8230; } \u5f02\u6b65\u65b9\u6cd5\u7684\u8fd4\u56de\u7c7b\u578b \u5f02\u6b65\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u53ea\u80fd\u4e3a: void\u3001Task\u3001Task\u3002 Task: \u4ee3\u8868\u4e00\u4e2a\u8fd4\u56de\u503cT\u7c7b\u578b\u7684\u64cd\u4f5c\u3002 Task: \u4ee3\u8868\u4e00\u4e2a\u65e0\u8fd4\u56de\u503c\u7684\u64cd\u4f5c\u3002 void: \u4e3a\u4e86\u548c\u4f20\u7edf\u7684\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u517c\u5bb9\u800c\u8bbe\u8ba1\u3002 await(\u7b49\u5f85) await\u7b49\u5f85\u7684\u662f\u4ec0\u4e48? \u53ef\u4ee5\u662f\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c(Task)\u3001\u4ea6\u6216\u8005\u662f\u5177\u5907\u8fd4\u56de\u503c\u7684\u5f02\u6b65\u64cd\u4f5c(Task)\u7684\u503c, \u5982\u4e0b: public async static void GetInfoAsync() { await GetData(); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,12],"tags":[],"class_list":["post-19","post","type-post","status-publish","format-standard","hentry","category-csharp","category-oop"],"_links":{"self":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts\/19","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=19"}],"version-history":[{"count":0,"href":"https:\/\/diji.net\/index.php?rest_route=\/wp\/v2\/posts\/19\/revisions"}],"wp:attachment":[{"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=19"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/diji.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}