{"id":532,"date":"2023-09-16T16:26:01","date_gmt":"2023-09-16T08:26:01","guid":{"rendered":""},"modified":"2023-09-17T00:41:54","modified_gmt":"2023-09-16T16:41:54","slug":"c%e5%a4%9a%e7%ba%bf%e7%a8%8b%e5%90%8c%e6%ad%a5%e6%89%a7%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/mushiming.com\/532.html","title":{"rendered":"c#\u591a\u7ebf\u7a0b\u540c\u6b65\u6267\u884c"},"content":{"rendered":"
\u5bf9\u4e8e\u591a\u7ebf\u7a0b\u540c\u6b65\u6267\u884c\uff0c\u53ef\u80fd\u542c\u8d77\u6765\u6bd4\u8f83\u62d7\u53e3\uff0c\u4f46\u662f\u5b83\u7684\u4f5c\u7528\u5728\u67d0\u4e9b\u4e1a\u52a1\u4e0b\uff0c\u6709\u975e\u5e38\u7684\u5927\u4f5c\u7528\uff0c\u5206\u522b\u8fdb\u884c\u9610\u8ff0\u3002<\/p>\n
\u540c\u6b65<\/p>\n
\u540c\u6b65\u5c31\u662f\u6309\u7167\u987a\u5e8f1234\u8fd9\u6837\u8fdb\u884c\u4e0b\u53bb\uff0c\u6709\u987a\u5e8f\uff0c\u6709\u89c4\u5f8b\u7684\u8fdb\u884c\u7740\u3002<\/p>\n
\u591a\u7ebf\u7a0b<\/p>\n
\u591a\u7ebf\u7a0b\u5bf9\u4e8e\u5355\u7ebf\u7a0b\u6765\u8bf4\u7684\uff0c\u5355\u7ebf\u7a0b\uff0c\u5c31\u662f\u5f00\u542f\u4e00\u4e2a\u7ebf\u7a0b\u8fdb\u884c\u5de5\u4f5c\uff0c\u591a\u7ebf\u7a0b\u5219\u662f\uff0c\u5f00\u542f\u591a\u4e2a\u7ebf\u7a0b\u8fdb\u884c\u591a\u4e2a\u5de5\u4f5c\uff0c\u5e76\u4e14\u6ca1\u6709\u987a\u5e8f\uff0c\u6ca1\u6709\u89c4\u5f8b\u6027\u3002<\/p>\n
\u591a\u7ebf\u7a0b\u540c\u6b65\uff0c\u5219\u662f\uff0c\u65e2\u6709\u540c\u6b65\u548c\u591a\u7ebf\u7a0b\u7684\u7279\u70b9\uff0c\u5373\u5f00\u542f\u591a\u4e2a\u7ebf\u7a0b\u8fdb\u884c\u5de5\u4f5c\u7684\u540c\u65f6\uff0c\u8fd8\u8981\u6309\u7167\u987a\u5e8f\u548c\u89c4\u5f8b\u6267\u884c\u3002<\/p>\n
\u5176\u5b9e\uff0c\u89e3\u51b3\u591a\u7ebf\u6027\u540c\u6b65\u7684\u95ee\u9898\uff0c\u6709\u5f88\u591a\u79cd\uff0c\u6bd4\u5982\u4f7f\u7528lock\u9501\u7b49\u7b49\u65b9\u6cd5\u3002\u672c\u8282\u4ecb\u7ecd\u4f7f\u7528async\/await\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u4ed6\u7684\u529f\u80fd\u3002<\/p>\n
1.\u5efa\u7acb\u4e00\u4e2awinform\u7a0b\u5e8f\uff0c\u53ef\u4ee5\u6a21\u62df\u754c\u9762\u5904\u7406\u6570\u636e<\/p>\n
<\/p>\n
2.\u4f7f\u7528\u540c\u6b65\u7684\u65b9\u6cd5<\/p>\n
\u5bf9\u53d8\u91cfa\u589e\u52a01\uff0c\u5176\u4e2d Thread.Sleep(1000)\u53ef\u4ee5\u8868\u793a\uff0c\u8fd9\u4e2a\u4e1a\u52a1\u5904\u7406\u7684\u65f6\u95f4\uff0c\u7136\u540e\u8fde\u7eed\u589e\u52a05\u6b21\u3002<\/p>\n
\u4ee3\u7801<\/p>\n
using System;\nusing System.Collections.Generic;\nusing System.ComponentModel;\nusing System.Data;\nusing System.Diagnostics;\nusing System.Drawing;\nusing System.Linq;\nusing System.Text;\nusing System.Threading;\nusing System.Threading.Tasks;\nusing System.Windows.Forms;\n\nnamespace WindowsFormsApp1\n{\n public partial class Form1 : Form\n {\n int a = 0;\n public Form1()\n {\n InitializeComponent();\n }\n private void button1_Click(object sender, EventArgs e)\n {\n Stopwatch stopwatch = new Stopwatch();\n stopwatch.Start();\n A();\n \/\/Task.Delay(3000);\n \/\/B();\n \/\/Task.Delay(3000);\n \/\/await C();\n stopwatch.Stop();\n label1.Text = a.ToString();\n label2.Text = \"\u8017\u65f6\uff1a\" + stopwatch.ElapsedMilliseconds;\n\n \/\/Thread.Sleep(10000);\n }\n \/\/\/ <summary>\n \/\/\/ \u5355\u7ebf\u7a0b\uff0c\u540c\u6b65\u6709\u987a\u5e8f\n \/\/\/ <\/summary>\n public void A()\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(1000);\n\n a++; Console.WriteLine(a);\n Thread.Sleep(1000);\n\n a++; Console.WriteLine(a);\n Thread.Sleep(1000);\n\n a++; Console.WriteLine(a);\n Thread.Sleep(1000);\n\n a++; Console.WriteLine(a);\n Thread.Sleep(1000);\n }\n \/\/\/ <summary>\n \/\/\/ \u591a\u7ebf\u7a0b\uff0c\u5e76\u884c\uff0c\u6ca1\u6709\u987a\u5e8f\n \/\/\/ <\/summary>\n public void B()\n {\n\n Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(10000);\n });\n Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(10000);\n });\n\n Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(10000);\n });\n\n Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(10000);\n });\n Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(10000);\n });\n\n\n \/\/List<Task> tasks = new List<Task>();\n\n\n \/\/tasks.Add(Task.Run(() =>\n \/\/{\n \/\/ a++;\n \/\/ Console.WriteLine(a);\n \/\/ Thread.Sleep(1000);\n \/\/}));\n\n \/\/tasks.Add(Task.Run(() =>\n \/\/{\n \/\/ a++;\n \/\/ Console.WriteLine(a);\n \/\/ Thread.Sleep(1000);\n \/\/}));\n\n \/\/tasks.Add(Task.Run(() =>\n \/\/{\n \/\/ a++;\n \/\/ Console.WriteLine(a);\n \/\/ Thread.Sleep(1000);\n \/\/}));\n\n \/\/tasks.Add(Task.Run(() =>\n \/\/{\n \/\/ a++;\n \/\/ Console.WriteLine(a);\n \/\/ Thread.Sleep(1000);\n \/\/}));\n\n \/\/tasks.Add(Task.Run(() =>\n \/\/{\n \/\/ a++;\n \/\/ Console.WriteLine(a);\n \/\/ Thread.Sleep(1000);\n \/\/}));\n\n \/\/Task.WaitAll(tasks.ToArray());\n }\n\n \/\/\/ <summary>\n \/\/\/ \u591a\u7ebf\u7a0b\uff0c\u5e76\u884c\uff0c\u6709\u987a\u5e8f\n \/\/\/ <\/summary>\n \/\/\/ <returns><\/returns>\n public async Task C()\n {\n Task task = Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(1000);\n });\n await task;\n Task task1 = Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(1000);\n });\n await task1;\n\n Task task2 = Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(1000);\n });\n await task2;\n\n Task task3 = Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(1000);\n });\n await task3;\n\n Task task4 = Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(1000);\n });\n await task4;\n }\n\n\n }\n}\n<\/code><\/pre>\n\u6548\u679c<\/p>\n
<\/p>\n
\u8017\u65f65036\uff0c\u6309\u7167\u987a\u5e8f\u6267\u884c\uff0c\u5e76\u4e14\u754c\u9762\u662f\u5361\u6b7b\u72b6\u6001\u3002<\/p>\n
3.\u4f7f\u7528\u591a\u7ebf\u7a0b\u7684\u65b9\u6cd5<\/p>\n
\u56e0\u4e3a\u53d8\u5316\u592a\u5feb\u4e86\uff0c\u6240\u4ee5\u589e\u52a0\u4e86\u5ef6\u8fdf<\/p>\n
\u591a\u7ebf\u7a0b\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528B()\u4e2d\u5c4f\u853d\u7684\u65b9\u6cd5<\/p>\n
\u4ee3\u7801<\/p>\n
using System;\nusing System.Collections.Generic;\nusing System.ComponentModel;\nusing System.Data;\nusing System.Diagnostics;\nusing System.Drawing;\nusing System.Linq;\nusing System.Text;\nusing System.Threading;\nusing System.Threading.Tasks;\nusing System.Windows.Forms;\n\nnamespace WindowsFormsApp1\n{\n public partial class Form1 : Form\n {\n int a = 0;\n public Form1()\n {\n InitializeComponent();\n }\n private void button1_Click(object sender, EventArgs e)\n {\n Stopwatch stopwatch = new Stopwatch();\n stopwatch.Start();\n \/\/A();\n Task.Delay(3000);\n B();\n Task.Delay(3000);\n \/\/await C();\n stopwatch.Stop();\n label1.Text = a.ToString();\n label2.Text = \"\u8017\u65f6\uff1a\" + stopwatch.ElapsedMilliseconds;\n\n \/\/Thread.Sleep(10000);\n }\n \/\/\/ <summary>\n \/\/\/ \u5355\u7ebf\u7a0b\uff0c\u540c\u6b65\u6709\u987a\u5e8f\n \/\/\/ <\/summary>\n public void A()\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(1000);\n\n a++; Console.WriteLine(a);\n Thread.Sleep(1000);\n\n a++; Console.WriteLine(a);\n Thread.Sleep(1000);\n\n a++; Console.WriteLine(a);\n Thread.Sleep(1000);\n\n a++; Console.WriteLine(a);\n Thread.Sleep(1000);\n }\n \/\/\/ <summary>\n \/\/\/ \u591a\u7ebf\u7a0b\uff0c\u5e76\u884c\uff0c\u6ca1\u6709\u987a\u5e8f\n \/\/\/ <\/summary>\n public void B()\n {\n Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(10000);\n });\n Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(10000);\n });\n\n Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(10000);\n });\n\n Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(10000);\n });\n Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(10000);\n });\n\n\n \/\/List<Task> tasks = new List<Task>();\n\n\n \/\/tasks.Add(Task.Run(() =>\n \/\/{\n \/\/ a++;\n \/\/ Console.WriteLine(a);\n \/\/ Thread.Sleep(1000);\n \/\/}));\n\n \/\/tasks.Add(Task.Run(() =>\n \/\/{\n \/\/ a++;\n \/\/ Console.WriteLine(a);\n \/\/ Thread.Sleep(1000);\n \/\/}));\n\n \/\/tasks.Add(Task.Run(() =>\n \/\/{\n \/\/ a++;\n \/\/ Console.WriteLine(a);\n \/\/ Thread.Sleep(1000);\n \/\/}));\n\n \/\/tasks.Add(Task.Run(() =>\n \/\/{\n \/\/ a++;\n \/\/ Console.WriteLine(a);\n \/\/ Thread.Sleep(1000);\n \/\/}));\n\n \/\/tasks.Add(Task.Run(() =>\n \/\/{\n \/\/ a++;\n \/\/ Console.WriteLine(a);\n \/\/ Thread.Sleep(1000);\n \/\/}));\n\n \/\/Task.WaitAll(tasks.ToArray());\n }\n\n \/\/\/ <summary>\n \/\/\/ \u591a\u7ebf\u7a0b\uff0c\u5e76\u884c\uff0c\u6709\u987a\u5e8f\n \/\/\/ <\/summary>\n \/\/\/ <returns><\/returns>\n public async Task C()\n {\n Task task = Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(1000);\n });\n await task;\n Task task1 = Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(1000);\n });\n await task1;\n\n Task task2 = Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(1000);\n });\n await task2;\n\n Task task3 = Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(1000);\n });\n await task3;\n\n Task task4 = Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(1000);\n });\n await task4;\n }\n\n\n }\n}\n<\/code><\/pre>\n\u6548\u679c <\/p>\n
<\/p>\n
\u8017\u65f629\uff0c\u53ef\u4ee5\u770b\u5230\u6ca1\u6709\u6309\u7167\u987a\u5e8f\u8fdb\u884c\uff0c\u4f46\u662f\u754c\u9762\u662f\u53ef\u4ee5\u79fb\u52a8\u7684<\/p>\n
4. \u591a\u7ebf\u7a0b\u540c\u6b65<\/p>\n
\u8fd9\u6bb5\u4ee3\u7801\u5c31\u9610\u8ff0\u4e86\uff0c\u5373\u5f00\u542f\u591a\u4e2a\u7ebf\u7a0b\u8fdb\u884c\u5de5\u4f5c\u7684\u540c\u65f6\uff0c\u8fd8\u6309\u7167\u987a\u5e8f\u548c\u89c4\u5f8b\u6267\u884c\u3002<\/p>\n
\u4ee3\u7801<\/p>\n
using System;\nusing System.Collections.Generic;\nusing System.ComponentModel;\nusing System.Data;\nusing System.Diagnostics;\nusing System.Drawing;\nusing System.Linq;\nusing System.Text;\nusing System.Threading;\nusing System.Threading.Tasks;\nusing System.Windows.Forms;\n\nnamespace WindowsFormsApp1\n{\n public partial class Form1 : Form\n {\n int a = 0;\n public Form1()\n {\n InitializeComponent();\n }\n private async void button1_Click(object sender, EventArgs e)\n {\n Stopwatch stopwatch = new Stopwatch();\n stopwatch.Start();\n \/\/A();\n \/\/Task.Delay(3000);\n \/\/B();\n \/\/Task.Delay(3000);\n await C();\n stopwatch.Stop();\n label1.Text = a.ToString();\n label2.Text = \"\u8017\u65f6\uff1a\" + stopwatch.ElapsedMilliseconds;\n\n \/\/Thread.Sleep(10000);\n }\n \/\/\/ <summary>\n \/\/\/ \u5355\u7ebf\u7a0b\uff0c\u540c\u6b65\u6709\u987a\u5e8f\n \/\/\/ <\/summary>\n public void A()\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(1000);\n\n a++; Console.WriteLine(a);\n Thread.Sleep(1000);\n\n a++; Console.WriteLine(a);\n Thread.Sleep(1000);\n\n a++; Console.WriteLine(a);\n Thread.Sleep(1000);\n\n a++; Console.WriteLine(a);\n Thread.Sleep(1000);\n }\n \/\/\/ <summary>\n \/\/\/ \u591a\u7ebf\u7a0b\uff0c\u5e76\u884c\uff0c\u6ca1\u6709\u987a\u5e8f\n \/\/\/ <\/summary>\n public void B()\n {\n Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(10000);\n });\n Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(10000);\n });\n\n Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(10000);\n });\n\n Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(10000);\n });\n Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(10000);\n });\n\n\n \/\/List<Task> tasks = new List<Task>();\n\n\n \/\/tasks.Add(Task.Run(() =>\n \/\/{\n \/\/ a++;\n \/\/ Console.WriteLine(a);\n \/\/ Thread.Sleep(1000);\n \/\/}));\n\n \/\/tasks.Add(Task.Run(() =>\n \/\/{\n \/\/ a++;\n \/\/ Console.WriteLine(a);\n \/\/ Thread.Sleep(1000);\n \/\/}));\n\n \/\/tasks.Add(Task.Run(() =>\n \/\/{\n \/\/ a++;\n \/\/ Console.WriteLine(a);\n \/\/ Thread.Sleep(1000);\n \/\/}));\n\n \/\/tasks.Add(Task.Run(() =>\n \/\/{\n \/\/ a++;\n \/\/ Console.WriteLine(a);\n \/\/ Thread.Sleep(1000);\n \/\/}));\n\n \/\/tasks.Add(Task.Run(() =>\n \/\/{\n \/\/ a++;\n \/\/ Console.WriteLine(a);\n \/\/ Thread.Sleep(1000);\n \/\/}));\n\n \/\/Task.WaitAll(tasks.ToArray());\n }\n\n \/\/\/ <summary>\n \/\/\/ \u591a\u7ebf\u7a0b\uff0c\u5e76\u884c\uff0c\u6709\u987a\u5e8f\n \/\/\/ <\/summary>\n \/\/\/ <returns><\/returns>\n public async Task C()\n {\n Task task = Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(1000);\n });\n await task;\n Task task1 = Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(1000);\n });\n await task1;\n\n Task task2 = Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(1000);\n });\n await task2;\n\n Task task3 = Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(1000);\n });\n await task3;\n\n Task task4 = Task.Run(() =>\n {\n a++;\n Console.WriteLine(a);\n Thread.Sleep(1000);\n });\n await task4;\n }\n\n\n }\n}\n<\/code><\/pre>\n \u6548\u679c<\/p>\n
<\/p>\n
\u8017\u65f65052\uff0c\u5e76\u4e14\u662f\u6309\u7167\u987a\u5e8f\u6267\u884c\u7684\uff0c\u800c\u4e14\u754c\u9762\u8fd8\u53ef\u4ee5\u62d6\u52a8\uff0c\u6b63\u662f\u6211\u4eec\u9700\u8981\u7684\u3002 <\/p>\n
\u62d3\u5c55async\/await<\/p>\n
\u5728C()\u65b9\u6cd5\u4e2d\uff0c\u5176\u5b9e\u662f\u9047\u5230await\u540e\uff0c\u4ee3\u7801\u5219\u662f\u8fd4\u56detask.run\u4e0a\u9762\u53bb\u4e86\uff0c\u7136\u540e\u4ece\u4e0a\u5230\u4e0b\u518d\u6267\u884c\uff0c\u6267\u884c\u5230\u7b2c\u4e8c\u65b9\u6cd5\uff0c\u53c8\u8fd4\u56detask.run\u4e0a\u9762\u53bb\u4e86\uff0c\u7136\u540e\u518d\u4ece\u4e0a\u5230\u4e0b\u6267\u884c\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u6240\u4ee5\u548c\u540c\u6b65\u6267\u884c\u7684\u65f6\u95f4\u57fa\u672c\u4e0a\u4e00\u6837\u7684\u3002<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"c#\u591a\u7ebf\u7a0b\u540c\u6b65\u6267\u884c\u5728C()\u65b9\u6cd5\u4e2d\uff0c\u5176\u5b9e\u662f\u9047\u5230await\u540e\uff0c\u4ee3\u7801\u5219\u662f\u8fd4\u56detask.run\u4e0a\u9762\u53bb\u4e86\uff0c\u7136\u540e\u4ece\u4e0a\u5230\u4e0b\u518d\u6267\u884c\uff0c\u6267\u884c\u5230\u7b2c\u4e8c\u65b9\u6cd5\uff0c\u53c8...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/532"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=532"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/532\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}