{"id":1859,"date":"2020-04-26T20:52:26","date_gmt":"2020-04-26T12:52:26","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=1859"},"modified":"2020-09-26T22:46:53","modified_gmt":"2020-09-26T14:46:53","slug":"c%e5%a4%9a%e4%b8%aa%e7%ba%bf%e7%a8%8b%e7%9a%84%e7%ad%89%e5%be%85","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=1859","title":{"rendered":"[C#]\u591a\u4e2a\u7ebf\u7a0b\u7684\u7b49\u5f85"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>using System.Collections.Generic;\nusing UnityEngine;\nusing System.Threading;\n\npublic class ThreadTestProj : MonoBehaviour\n{\n    [ContextMenu(\"\u5f00\u59cb\u6267\u884c\")]\n    void StartExecute()\n    {\n        Debug.Log(\"PrepareExecute\");\n        var waits = new List&lt;EventWaitHandle>();\n        for(int i=0; i&lt;2; ++i)\n        {\n            var mr = new ManualResetEvent(false);\n            waits.Add(mr);\n            Thread thread = new Thread(ExecuteThreadBody);\n            thread.Start(mr);\n        }\n        Debug.Log(\"WaitAll...\");\n        WaitHandle.WaitAll(waits.ToArray());\n        Debug.Log(\"Execute.Finish\");\n    }\n\n    void ExecuteThreadBody(object obj)\n    {\n        ManualResetEvent p = obj as ManualResetEvent;\n        Thread.Sleep(3000);\n        Debug.Log(\"Body.Execute\"); \/\/ \u8fd9\u53e5\u53ef\u80fd\u4f1a\u5361\u6b7b\uff01\n        p.Set();\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\u4e3a:<\/p>\n\n\n\n<p>PrepareExecute<br>Body.Execute<br>Body.Execute <br>WaitAll&#8230;<br>Execute.Finish<\/p>\n\n\n\n<p>\u5728\u5b50\u7ebf\u7a0b\u4e2d\uff0c\u4e0d\u8981\u64cd\u4f5c\u4efb\u4f55Unity\u5bf9\u8c61\uff0c\u5305\u62ec\u65e5\u5fd7\u8f93\u51fa<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f93\u51fa\u4e3a: PrepareExecuteBody.ExecuteBody.Execute WaitAll&#038;#82 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/1859"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1859"}],"version-history":[{"count":2,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/1859\/revisions"}],"predecessor-version":[{"id":1861,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/1859\/revisions\/1861"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1859"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1859"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1859"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}