{"id":762,"date":"2016-08-25T16:52:48","date_gmt":"2016-08-25T07:52:48","guid":{"rendered":"http:\/\/gomocool.net\/gomokulog\/?p=762"},"modified":"2016-08-25T16:52:48","modified_gmt":"2016-08-25T07:52:48","slug":"c%e3%81%96%e3%81%a3%e3%81%8f%e3%82%8a%e3%83%9e%e3%83%ab%e3%83%81%e3%82%b9%e3%83%ac%e3%83%83%e3%83%89%e9%9d%9e%e5%90%8c%e6%9c%9f%e5%87%a6%e7%90%86","status":"publish","type":"post","link":"https:\/\/gomokulog.gomocool.net\/?p=762","title":{"rendered":"[C#]\u3056\u3063\u304f\u308a\u30de\u30eb\u30c1\u30b9\u30ec\u30c3\u30c9(\u975e\u540c\u671f\u51e6\u7406)"},"content":{"rendered":"<p>C#\u306e\u30de\u30eb\u30c1\u30b9\u30ec\u30c3\u30c9(\u975e\u540c\u671f\u51e6\u7406)\u306b\u95a2\u3057\u3066\u3001\u3044\u3064\u3082\u306e\u3088\u3046\u306b\u300c\u3056\u3063\u304f\u308a\u300d\u8aac\u660e\u3002<\/p>\n<p>C#\u306e\u30de\u30eb\u30c1\u30b9\u30ec\u30c3\u30c9\u306f\u3001\u4f55\u7a2e\u985e\u304b\u66f8\u304d\u65b9\u304c\u3042\u308b\u304c\u3001\u4e3b\u306b4\u3064\u306e\u30d1\u30bf\u30fc\u30f3\u304c\u3042\u308b\u3002<\/p>\n<ol>\n<li>Thread\u3067\u30c7\u30ea\u30b2\u30fc\u30c8\u3092\u52d5\u304b\u3059\u3002<br \/>\n\u57fa\u672c\u3002\u3067\u3082\u3001.NetFramework4\u4ee5\u964d\u3067\u306f\u4f7f\u308f\u306a\u3044\u3068\u601d\u3046\u3002<\/li>\n<li>ThreadPool\u30671\u3092\u3082\u3046\u5c11\u3057\u52b9\u7387\u3088\u304f\u3059\u308b\u3002<br \/>\n1\u306e\u65b9\u6cd5\u3060\u3068\u3001\u30b9\u30ec\u30c3\u30c9\u6570\u304c\u591a\u304f\u306a\u3063\u305f\u308a\u3059\u308b\u3068\u3001\u5834\u5408\u306b\u3088\u3063\u3066\u306f\u9006\u306b\u9045\u304f\u306a\u308b\u3002<br \/>\n\u306a\u306e\u3067ThreadPool\u3092\u4f7f\u3046\u3053\u3068\u3067\u52b9\u7387\u30a2\u30c3\u30d7\u3067\u304d\u308b\u304c\u3001\u66f8\u304d\u65b9\u304c\u3081\u3093\u3069\u3044\u3057\u3001\u306a\u3093\u3060\u304b\u5206\u304b\u3089\u306a\u304f\u306a\u308b\u3002<\/li>\n<li>Task\u30af\u30e9\u30b9\u306b1\u3068\u304b2\u306e\u4e2d\u8eab\u3092\u304a\u4efb\u305b (.Net Framework4)<br \/>\n1\u306e\u611f\u899a\u3067\u88cf\u3067\u306fThreadPool\u3092\u4f7f\u3063\u3066\u304f\u308c\u308b\u3002<br \/>\n.NetFramework4\u4ee5\u964d\u306f\u3053\u308c\u3060\u3051\u899a\u3048\u3066\u308c\u3070\u3088\u3044\u3002<\/li>\n<li>async await \u3067\u3001\u30b7\u30f3\u30d7\u30eb\u975e\u540c\u671f\u51e6\u7406 (.Net Framework4.5)<br \/>\n\u30b7\u30f3\u30d7\u30eb\u306a\u8a18\u8ff0\u3067\u975e\u540c\u671f\u51e6\u7406\u3092\u66f8\u3051\u308b\u3002GUI\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067Invoke\u3067\u30b9\u30ec\u30c3\u30c9\u30bb\u30fc\u30d5\u306a\u66f8\u304d\u65b9\u304c\u7c21\u6f54\u306b\u306a\u308b\u3002<\/li>\n<\/ol>\n<p>\u3093\u3067\u3001\u3068\u308a\u3042\u3048\u305a\u3061\u3083\u3093\u3068\u899a\u3048\u308b\u3079\u304d\u306f\u30013\u306e\u65b9\u6cd5\u304b\u306a\uff5e\u3063\u3068\u3002<\/p>\n<p>3\u304c\u7406\u89e3\u3067\u304d\u305f\u3089\u3001\u4f7f\u3048\u308b\u30dd\u30a4\u30f3\u30c8\u30674\u306easync, await\u3092\u66f8\u304f\u3068\u3001\u4fbf\u5229\u3060\u306a\u30fc\u3063\u3066\u601d\u3046\u3068\u304d\u304c\u3042\u308b\u3002<\/p>\n<p>1.\u306f\u899a\u3048\u308b\u5fc5\u8981\u306f\u7121\u3044\u3051\u3069\u3001\u6b74\u53f2\u3068\u3057\u3066\u8efd\u304f\u77e5\u3063\u3066\u304a\u304f\u3068\u3088\u3044\u3002(.NETFramework4\u672a\u6e80\u306a\u5834\u5408\u306f\u304c\u3093\u3070\u308b\u3057\u304b\u306a\u3044\u3002)<\/p>\n<p>2.\u306f\u4ed5\u7d44\u307f\u306f\u77e5\u308b\u5fc5\u8981\u304c\u3042\u308b\u3051\u3069\u3001\u66f8\u304d\u65b9\u306f\u3082\u3046\u899a\u3048\u306a\u304f\u3066\u3044\u3044\u3058\u3083\u306a\u3044\u304b\u306a\u30fb\u30fb\u30fb\u3002<\/p>\n<p>\u306a\u306e\u3067\u3001\u3081\u3093\u3069\u304f\u3055\u3044\u65b9\u3084\u304a\u6642\u9593\u7121\u3044\u65b9\u306f\u3001Task\u3068\u30b9\u30ec\u30c3\u30c9\u30bb\u30fc\u30d5\u306e\u8fba\u308a\u3060\u3051\u304b\u3044\u3064\u307e\u3081\u3070\u3088\u3044\u304b\u3068\uff01<\/p>\n<h2>Thread<\/h2>\n<p>\u3067\u3001\u307e\u305a\u306f\u6b74\u53f2\u3068\u3057\u3066\u8efd\u304f\u899a\u3048\u3066\u304a\u304f\u3079\u304dThread\u306b\u3064\u3044\u3066\u3001\u8efd\u304f\u3002<br \/>\n\u4ee5\u4e0b\u306e\u4f8b\u306f\u30b3\u30f3\u30bd\u30fc\u30eb\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3067\u3001Thread\u4f7f\u3063\u305f\u5834\u5408\u3002<\/p>\n<pre class=\"lang:c# decode:true\">public void Run()\r\n{\r\n\tThread thread = new Thread(\r\n\t\t() =&gt; {\r\n\t\t\tfor(int i = 0; i &lt; 5; i++)\r\n\t\t\t{\r\n\t\t\t\tConsole.WriteLine(\"\u5225\u30b9\u30ec\u30c3\u30c9\u3060\u3088\");\r\n\t\t\t\tThread.Sleep(1000);\r\n\t\t\t}\r\n\t\t});\r\n\tthread.Start();\r\n\r\n\tfor (int i = 0; i &lt; 5; i++)\r\n\t{\r\n\t\tConsole.WriteLine(\"\u672c\u4f53\u30b9\u30ec\u30c3\u30c9\u3060\u3088\");\r\n\t\tThread.Sleep(1000);\r\n\t}\r\n\tConsole.ReadKey();\r\n}<\/pre>\n<p>\u3093\u3067\u3001\u7d50\u679c\u3002<br \/>\n<a href=\"http:\/\/gomocool.net\/gomokulog\/wp-content\/uploads\/2016\/06\/mt1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-763\" src=\"http:\/\/gomocool.net\/gomokulog\/wp-content\/uploads\/2016\/06\/mt1.png\" alt=\"mt1\" width=\"149\" height=\"197\" \/><\/a><\/p>\n<p>\u547c\u3073\u51fa\u3057\u305f\u5074\u306e\u672c\u4f53\u306e\u30b9\u30ec\u30c3\u30c9\u3068\u3001Thread\u30af\u30e9\u30b9\u3067\u4f5c\u3063\u305f\u5225\u30b9\u30ec\u30c3\u30c9\u306e2\u672c\u304c\u540c\u6642\u306b\u52d5\u304d\u307e\u3059\u3002<\/p>\n<p>Thread\u306e\u5f15\u6570\u306f\u30c7\u30ea\u30b2\u30fc\u30c8\u306a\u306e\u3067\u3001\u5b9f\u884c\u3055\u305b\u305f\u3044\u95a2\u6570\u3092\u6301\u305f\u305b\u308b\u304b\u3001\u533f\u540d\u30e1\u30bd\u30c3\u30c9\u3067\u305d\u306e\u5834\u3067\u30e1\u30bd\u30c3\u30c9\u3092\u66f8\u304f\u3002<br \/>\n\u4eca\u56de\u306f\u30e9\u30e0\u30c0\u5f0f\u3067\u305d\u306e\u5834\u3067\u51e6\u7406\u66f8\u3044\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3064\u307e\u308a\u3001\u30de\u30eb\u30c1\u30b9\u30ec\u30c3\u30c9\u306f\u3001\u30c7\u30ea\u30b2\u30fc\u30c8\u306b\u899a\u3048\u3055\u305b\u305f\u95a2\u6570\u3092\u5225\u30b9\u30ec\u30c3\u30c9\u3067\u5b9f\u884c\u3055\u305b\u308b\u4ed5\u7d44\u307f\u3002<br \/>\n\u3060\u304b\u3089\u3001\u30c7\u30ea\u30b2\u30fc\u30c8\u306f\u3056\u3063\u304f\u308a\u3068\u3067\u3082\u7406\u5c48\u3092\u899a\u3048\u3068\u304b\u306a\u3044\u3068\u306d\u3002<br \/>\n\u30c7\u30ea\u30b2\u30fc\u30c8\u306e\u8a18\u4e8b\u306f\u524d\u306b\u66f8\u3044\u3066\u3044\u308b\u306e\u3067\u76ee\u3092\u901a\u3057\u305f\u65b9\u304c\u3088\u3044\u304b\u3082\u3002<br \/>\n<a href=\"http:\/\/gomocool.net\/gomokulog\/?p=250\">\u30c7\u30ea\u30b2\u30fc\u30c8\u3068\u306f\u306a\u3093\u305e\u3084<\/a><\/p>\n<p>\u3053\u308c\u304c\u57fa\u672c\u306e\u30de\u30eb\u30c1\u30b9\u30ec\u30c3\u30c9(\u975e\u540c\u671f\u51e6\u7406)\u306b\u306a\u308b\u3002<\/p>\n<h2>ThreadPool(\u30b9\u30ec\u30c3\u30c9\u30d7\u30fc\u30eb)<\/h2>\n<p>\u30b9\u30ec\u30c3\u30c9\u306e\u4f5c\u6210\u306f\u5272\u3068\u91cd\u3044\u51e6\u7406\u3067\u3059\u3002<\/p>\n<p>\u74b0\u5883\u306b\u3088\u308b\u3068\u601d\u3046\u3093\u3067\u3059\u304c\u3001\u30b9\u30ec\u30c3\u30c9\u3092\u4f5c\u6210\u3059\u308b\u306e\u306b1\u79d2\u639b\u304b\u308b\u5834\u5408\u3082\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u3064\u307e\u308a1\u79d2\u304b\u304b\u3089\u306a\u3044\u51e6\u7406\u3092\u30de\u30eb\u30c1\u30b9\u30ec\u30c3\u30c9\u3067\u51e6\u7406\u3059\u308b\u3068\u4e0b\u624b\u3059\u308b\u3068\u9045\u304f\u306a\u308b\u3068\u3044\u3046\u6b8b\u5ff5\u306a\u4e8b\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u305d\u3053\u3067\u3001\u4f7f\u308f\u308c\u308b\u306e\u304c\u30b9\u30ec\u30c3\u30c9\u30d7\u30fc\u30eb(Thread Pool)<\/p>\n<p>\u30b9\u30ec\u30c3\u30c9\u30d7\u30fc\u30eb\u306f\u30b9\u30ec\u30c3\u30c9\u3092\u3044\u304f\u3064\u304b\u4e8b\u524d\u306b\u7acb\u3061\u4e0a\u3052\u3068\u3044\u3066\u3001\u4f7f\u3044\u307e\u308f\u3059\u3053\u3068\u3067\u3001\u30b9\u30ec\u30c3\u30c9\u4f5c\u6210\u306e\u6642\u9593\u3092\u77ed\u7e2e\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<\/p>\n<p>\u4eca\u56de\u306f\u30b3\u30fc\u30c9\u306f\u5272\u611b\u3002<\/p>\n<h2>Task<\/h2>\n<p>\u305d\u3057\u3066\u3001.NetFramework4\u3067Task\u30af\u30e9\u30b9\u304c\u3067\u304d\u3001Thread\u3084ThreadPool\u3092\u4e2d\u3067\u4e0a\u624b\u304f\u30b4\u30cb\u30e7\u30b4\u30cb\u30e7\u3057\u3066\u304f\u308c\u308b\u3088\u3046\u306b\u306a\u3063\u305f\u3002<\/p>\n<p>\u3055\u3063\u304d\u306e\u3001Thread\u3092Task\u3067\u66f8\u304d\u306a\u304a\u3059\u3068\u4ee5\u4e0b\u306e\u69d8\u306b\u306a\u308b\u3002<\/p>\n<pre class=\"lang:c# decode:true \">public void Run()\r\n{\r\n\tTask task = new Task(() =&gt;\r\n\t{\r\n\t\tfor (int i = 0; i &lt; 5; i++)\r\n\t\t{\r\n\t\t\tConsole.WriteLine(\"\u5225\u30b9\u30ec\u30c3\u30c9\u3060\u3088\");\r\n\t\t\tThread.Sleep(1000);\r\n\t\t}\r\n\t});\r\n\ttask.Start();\r\n\r\n\tfor (int i = 0; i &lt; 5; i++)\r\n\t{\r\n\t\tConsole.WriteLine(\"\u672c\u4f53\u30b9\u30ec\u30c3\u30c9\u3060\u3088\");\r\n\t\tThread.Sleep(1000);\r\n\t}\r\n\tConsole.ReadKey();\r\n}<\/pre>\n<p>\u307e\u3041\u3001\u307b\u3068\u3093\u3069\u5909\u308f\u3089\u306a\u3044\u3088\u306d\u3002<\/p>\n<p>\u4eca\u5f8c\u306fThread\u3067\u306a\u304fTask\u3067\u66f8\u304f\u3079\u304d\u3002<\/p>\n<p>\u3061\u306a\u307f\u306b\u3001Task\u3092\u8d70\u3089\u305b\u308b\u306b\u306f\u3001Task\u3092\u4f5c\u3063\u3066Start\u3092\u547c\u3076\u65b9\u6cd5\u3068\u3001\u3082\u3046\u3044\u304d\u306a\u308aStart\u3055\u305b\u308b\u65b9\u6cd5\u304c\u3042\u308b\u3002\u4e0a\u8a18\u306f\u524d\u8005\u3002<\/p>\n<p>\u7279\u306b\u7406\u7531\u304c\u306a\u3044\u306a\u3089\u3001\u3044\u304d\u306a\u308aStart\u3055\u305b\u308b\u65b9\u6cd5\u304c\u3088\u3055\u3052\u3002<\/p>\n<p>\u305d\u306e\u5834\u5408\u3001\u4ee5\u4e0b\u306e2\u3064\u304c\u3042\u308b\u3002\u4e0b\u306e\u65b9\u304c\u3088\u308a\u7d30\u304b\u306a\u8a2d\u5b9a\u304c\u3067\u304d\u308b\u304c\u3001\u3068\u308a\u3042\u3048\u305aRun\u4f7f\u3063\u3068\u3051\u3070\u3088\u3044\u3002<\/p>\n<pre class=\"lang:c# decode:true \">Task task1 = Task.Run(() =&gt; { Console.Write(\"Run\"); });\r\nTask task2 = Task.Factory.StartNew(() =&gt; { Console.Write(\"StartNew\"); });<\/pre>\n<p>&nbsp;<\/p>\n<p>\u3053\u3053\u307e\u3067\u306f\u7c21\u5358\u3067\u3059\u306d\u3002<\/p>\n<p>\u307e\u3041\u3001\u5f15\u6570\u3082\u306a\u3044\u3057\u3001\u6295\u3052\u3063\u3071\u306a\u3057\u3067OK\u306a\u306e\u3067<\/p>\n<h2>ThreadSafe(\u30b9\u30ec\u30c3\u30c9\u30bb\u30fc\u30d5)<\/h2>\n<p>\u3067\u3001\u30de\u30eb\u30c1\u30b9\u30ec\u30c3\u30c9\u306b\u306f\u3001\u30b9\u30ec\u30c3\u30c9\u30bb\u30fc\u30d5\u3068\u3044\u3046\u6982\u5ff5\u304c\u5b58\u5728\u3059\u308b\u3002<\/p>\n<p>\u30b9\u30ec\u30c3\u30c9\u30bb\u30fc\u30d5\u3068\u306f\u3001<strong>\u8907\u6570\u306e\u30b9\u30ec\u30c3\u30c9\u304c\u540c\u6642\u306b\u5b9f\u884c\u3057\u3066\u3082\u554f\u984c\u304c\u767a\u751f\u3057\u306a\u3044\u3053\u3068\u3002<\/strong>\u307f\u305f\u3044\u306a\u611f\u3058\u3002<\/p>\n<p>\u3069\u3046\u3044\u3046\u3053\u3068\u304b\u3068\u3044\u3046\u3068\u3001<\/p>\n<pre class=\"lang:c# decode:true\">int count = 0;\r\nTask task = new Task(() =&gt;\r\n{\r\n\t\/\/count \u306b +10 \u30925\u56de\u3067\u8a0850\u3092\u8ffd\u52a0\u3059\u308b\r\n\tfor (int i = 0; i &lt; 5; i++)\r\n\t{\r\n\t\tint tmpCount = count;\r\n\t\tConsole.WriteLine(\"\u5225\u30b9\u30ec\u30c3\u30c9\u3060\u3088\");\r\n\t\tcount = tmpCount + 10;\r\n\t}\r\n});\r\ntask.Start();\r\n\r\n\/\/count\u304b\u3089-5\u306e5\u56de\u3067\u8a08-25\u3092\u5f15\u304f\r\nfor (int i = 0; i &lt; 5; i++)\r\n{\r\n\tint tmpCount = count;\r\n\tConsole.WriteLine(\"\u672c\u4f53\u30b9\u30ec\u30c3\u30c9\u3060\u3088\");\r\n\tcount = tmpCount - 5;\r\n}\r\n\r\ntask.Wait();\t\/\/\u5225\u30b9\u30ec\u30c3\u30c9\u306e\u51e6\u7406\u3092\u5f85\u3064\r\n\r\n\/\/\u5408\u8a08\u306f50-25\u306725\uff1f\r\nConsole.WriteLine(count);\r\n\r\nConsole.ReadKey();<\/pre>\n<p>\u3053\u308c\u3092\u5b9f\u884c\u3059\u308b\u306825\u304c\u8fd4\u3063\u3066\u304d\u305d\u3046\u3060\u304c\u300150\u3060\u3063\u305f\u308a40\u3060\u3063\u305f\u308a25\u3060\u3063\u305f\u308a\u3068\u3001\u5b9f\u884c\u3059\u308b\u3054\u3068\u306b\u5024\u304c\u9055\u3046\u5834\u5408\u304c\u3042\u308b\u3002(\u4e0a\u8a18\u30b3\u30fc\u30c9\u3067\u518d\u73fe\u3057\u306a\u3044\u5834\u5408\u3082\u3042\u308a\u3002)<\/p>\n<p>2\u3064\u306e\u30b9\u30ec\u30c3\u30c9\u304c\u540c\u6642\u306b\u540c\u3058\u5024(count)\u3092\u66f4\u65b0\u3059\u308b\u306e\u3067\u3001\u5024\u3092\u66f4\u65b0\u3057\u3066\u3044\u308b\u9593\u306b\u5225\u30b9\u30ec\u30c3\u30c9\u3067\u66f4\u65b0\u3055\u308c\u3066\u3057\u307e\u3044\u5024\u304c\u3042\u3084\u3075\u3084\u306b\u306a\u308b\u3002<br \/>\n(tmpCount\u306b\u7121\u7406\u3084\u308a\u5165\u308c\u305f\u308a\u3057\u3066\u308b\u306e\u306f\u3001\u3042\u3084\u3075\u3084\u306b\u306a\u308a\u3084\u3059\u3044\u3088\u3046\u306b\u308f\u3056\u3068\u610f\u5473\u306e\u306a\u3044\u4ee3\u5165\u3092\u3057\u3066\u3044\u307e\u3059\u3002)<\/p>\n<p>\u3053\u3044\u3046\u3063\u305f\u72b6\u6cc1\u3092\u3001<strong>\u30b9\u30ec\u30c3\u30c9\u30bb\u30fc\u30d5\u3067\u306a\u3044<\/strong>\u3002\u3068\u3044\u3046\u3002<\/p>\n<p>\u3093\u3067\u3001\u3069\u3046\u3059\u308b\u304b\u3068\u3044\u3046\u3068\u3001\u4e00\u756a\u4e00\u822c\u7684\u306a\u65b9\u6cd5\u304c\u3001\u30ed\u30c3\u30af(lock)\u3068\u3044\u3046\u65b9\u6cd5\u3067\u66f4\u65b0\u306e\u969b\u306b\u3001count\u306e\u5024\u3092\u56fa\u5b9a\u3059\u308b\u65b9\u6cd5\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:c# decode:true\">Object lockObj = new Object();\r\nint count = 0;\r\n\r\nTask task = new Task(() =&gt;\r\n{\r\n\t\/\/count \u306b +10 \u30925\u56de\u3067\u8a0850\u3092\u8ffd\u52a0\u3059\u308b\r\n\tfor (int i = 0; i &lt; 5; i++)\r\n\t{\r\n\t\tThread.Sleep(i);\r\n\t\tlock (lockObj)\r\n\t\t{\r\n\t\t\tint tmpCount = count;\r\n\t\t\tConsole.WriteLine(\"\u5225\u30b9\u30ec\u30c3\u30c9\u3060\u3088\");\r\n\t\t\tcount = tmpCount + 10;\r\n\t\t}\r\n\t}\r\n});\r\ntask.Start();\r\n\r\n\/\/count\u304b\u3089-5\u306e5\u56de\u3067\u8a08-25\u3092\u5f15\u304f\r\nfor (int i = 0; i &lt; 5; i++)\r\n{\r\n\tThread.Sleep(5 - i);\r\n\tlock (lockObj)\r\n\t{\r\n\t\tint tmpCount = count;\r\n\t\tConsole.WriteLine(\"\u672c\u4f53\u30b9\u30ec\u30c3\u30c9\u3060\u3088\");\r\n\t\tcount = tmpCount - 5;\r\n\t}\r\n}\r\n\r\ntask.Wait();\r\n\r\n\/\/\u5408\u8a08\u306f50-25\u306725\uff1f\r\nConsole.WriteLine(count);<\/pre>\n<p>lockObj\u3068\u3044\u3046\u3001\u4f55\u3067\u3082\u306a\u3044\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u30a4\u30f3\u30b9\u30bf\u30f3\u30c8\u3092\u4f5c\u6210\u3002<\/p>\n<p>lock\u3067lockObj\u3092\u6301\u3063\u3066\u3044\u308b\u9593\u306f\u3001\u540c\u3058lockObje\u3092\u6301\u3063\u3066\u3044\u308b\u4ed6\u306elock\u306f\u5f85\u6a5f\u3068\u306a\u308b\u3002<\/p>\n<p>\u8907\u6570\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u307e\u305f\u3044\u3067\u4f7f\u7528\u3055\u308c\u308b\u5834\u5408\u306f\u3001private\u306e\u5909\u6570\u306a\u3069\u306blockObj\u3092\u6301\u305f\u305b\u308b\u3068\u3088\u3044\u304b\u3082\u3002<br \/>\n\u3053\u306e\u3088\u3046\u306b\u30de\u30eb\u30c1\u30b9\u30ec\u30c3\u30c9\u306f\u3001\u305d\u306e\u8a18\u8ff0\u304c<strong>\u30b9\u30ec\u30c3\u30c9\u30bb\u30fc\u30d5\u3067\u3042\u308b<\/strong>\u304b\u3092\u610f\u8b58\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<\/p>\n<p>\u3053\u306e\u90e8\u5206\u306b\u6c17\u3092\u4ed8\u3051\u3066\u3044\u306a\u3044\u3068\u3001\u300c\u3054\u304f\u7a00\u306b\u3001\u5024\u304c\u304a\u304b\u3057\u3044\u300d\u306a\u3069\u306e\u30d0\u30b0\u304c\u767a\u751f\u3059\u308b\u3002<br \/>\n(\u30b2\u30fc\u30e0\u306e\u3088\u3046\u306b\u8907\u6570\u306e\u6575\u3084\u30ad\u30e3\u30e9\u3092\u30de\u30eb\u30c1\u30b9\u30ec\u30c3\u30c9\u3067\u52d5\u304b\u3057\u3001\u304a\u4e92\u3044\u304c\u5e72\u6e09\u3057\u3042\u3046\u3088\u3046\u306a\u5834\u5408\u306a\u3069\u306f\u3001\u983b\u7e41\u306b\u8d77\u3053\u308a\u305d\u3046\u3067\u3059\u306d\u3002)<\/p>\n<h2>async \u3068 await<\/h2>\n<p>\u3055\u3066\u3001\u6700\u5f8c\u306basync\u3068await\u3092\u8efd\u304f\u3054\u8aac\u660e\u3002<\/p>\n<p>async\u3068await\u306f\u3001\u975e\u540c\u671f\u51e6\u7406\u3092\u610f\u8b58\u305b\u305a\u306b\u3001\u81ea\u7136\u306a\u5f62\u3067\u66f8\u3051\u308b\u3088\u3046\u306b\u306a\u308b\u3002<\/p>\n<p>\u307e\u305a\u3001\u9069\u5f53\u306bWindows Form \u3092\u4f5c\u6210\u3002<\/p>\n<p>\u30dc\u30bf\u30f3\u3068\u30e9\u30d9\u30eb\u3092\u914d\u7f6e\u3002<\/p>\n<p>\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3057\u305f\u3089\u91cd\u3044\u51e6\u7406(\u3053\u3053\u3067\u306f5\u79d2\u5f85\u3064)\u3092\u884c\u3063\u305f\u5f8c\u30e9\u30d9\u30eb\u3092\u66f4\u65b0\u3059\u308b\u3002<\/p>\n<pre class=\"lang:c# decode:true \">using System.Windows.Forms;\r\n\r\nnamespace asynawaitTest\r\n{\r\n\tpublic partial class Form1 : Form\r\n\t{\r\n\t\tpublic Form1()\r\n\t\t{\r\n\t\t\tInitializeComponent();\r\n\t\t}\r\n\r\n\t\tprivate void button1_Click(object sender, EventArgs e)\r\n\t\t{\r\n\t\t\tvar start = DateTime.Now;\r\n\r\n\t\t\t\/\/\u4f55\u3089\u304b\u306e\u91cd\u3044\u51e6\u7406\r\n\t\t\tSystem.Threading.Thread.Sleep(5000);\r\n\r\n\t\t\tvar ts = DateTime.Now - start;\r\n\r\n\t\t\tthis.label1.Text = \"OK:\" + ts.TotalSeconds + \"Sec\";\r\n\t\t}\r\n\t}\r\n}<\/pre>\n<p>\u3060\u304c\u3001\u3053\u306e\u4f5c\u308a\u306b\u306f\u554f\u984c\u304c\u3042\u308b\u3002<\/p>\n<p>\u30dc\u30bf\u30f3\u3092\u62bc\u3057\u3066\u3001\u51e6\u7406\u3092\u884c\u3063\u3066\u3044\u308b\u9593\u306f\u30e6\u30fc\u30b6\u30fc\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u5074\u304c\u4f55\u306e\u51e6\u7406\u3082\u53d7\u3051\u4ed8\u3051\u308c\u305a\u306b\u3001\u56fa\u307e\u3063\u3066\u3057\u307e\u3046(\u3069\u3093\u306a\u64cd\u4f5c\u3082\u53d7\u3051\u4ed8\u3051\u3089\u308c\u306a\u304f\u306a\u308b\u3002)\u3002<\/p>\n<p>\u3067\u3001\u4e00\u822c\u7684\u306b\u306f\u3069\u3046\u3059\u308b\u304b\u3068\u3044\u3046\u3068\u3001\u4ee5\u4e0b\u306e\u69d8\u306b\u5225\u306e\u30b9\u30ec\u30c3\u30c9\u3092\u7acb\u3061\u4e0a\u3052\u308b\u3053\u3068\u3067\u3001\u30e1\u30a4\u30f3\u30b9\u30ec\u30c3\u30c9\u306f\u56fa\u307e\u3089\u305a\u306b\u3059\u3080\u3002<\/p>\n<pre class=\"lang:c# decode:true \">private void button1_Click(object sender, EventArgs e)\r\n{\r\n\tTask.Factory.StartNew(() =&gt;\r\n\t{\r\n\t\tvar start = DateTime.Now;\r\n\r\n\t\t\/\/\u4f55\u3089\u304b\u306e\u91cd\u3044\u51e6\u7406\r\n\t\tSystem.Threading.Thread.Sleep(5000);\r\n\r\n\t\tvar ts = DateTime.Now - start;\r\n\r\n\t\tthis.label1.Text = \"OK:\" + ts.TotalSeconds + \"Sec\";\r\n\r\n\t});\r\n}<\/pre>\n<p>\u3057\u304b\u3057\u3001\u3053\u308c\u3067\u3084\u308b\u3068label\u304c\u66f4\u65b0\u3055\u308c\u306a\u3044\u3002(\u3082\u3057\u304f\u306f\u4f8b\u5916\u3067\u843d\u3061\u308b\u3002)<\/p>\n<p>\u3053\u308c\u306f\u3001GUI\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u7279\u5fb4\u3067\u3001\u5148\u306b\u8aac\u660e\u3057\u305f\u30b9\u30ec\u30c3\u30c9\u30bb\u30fc\u30d5\u304c\u7406\u7531\u3067\u3001UI\u30b9\u30ec\u30c3\u30c9\u547c\u3070\u308c\u308b\u30e1\u30a4\u30f3\u30b9\u30ec\u30c3\u30c9\u4ee5\u5916\u304b\u3089\u3001UI\u3092\u5909\u66f4\u3067\u304d\u306a\u3044\u3068\u3044\u3046\u5236\u7d04\u304c\u3042\u308b\u305f\u3081\u3067\u3042\u308b\u3002<\/p>\n<p>\u3093\u3067\u3001\u3069\u3046\u3059\u308b\u304b\u3063\u3066\u3044\u3046\u3068\u3001UI\u3092\u5909\u66f4\u3059\u308b\u5834\u5408\u306fUI\u30b9\u30ec\u30c3\u30c9\u306b\u304a\u9858\u3044\u3059\u308b\u3002\u305d\u308c\u304cInvoke\u3068\u547c\u3070\u308c\u308b\u6a5f\u80fd\u3002(Invoke: \u6551\u3044\u3092\u6c42\u3081\u3066\u547c\u3073\u304b\u3051\u308b\u3002\u7948\u9858\u3059\u308b\u3002)<\/p>\n<pre class=\"lang:c# decode:true \">private void button1_Click(object sender, EventArgs e)\r\n{\r\n\tTask.Factory.StartNew(() =&gt;\r\n\t{\r\n\t\tvar start = DateTime.Now;\r\n\r\n\t\t\/\/\u4f55\u3089\u304b\u306e\u91cd\u3044\u51e6\u7406\r\n\t\tSystem.Threading.Thread.Sleep(5000);\r\n\r\n\t\tvar ts = DateTime.Now - start;\r\n\r\n\t\tSafeSetLable(ts.TotalSeconds.ToString());\r\n\r\n\t});\r\n}\r\n\r\nprivate void SafeSetLable(string text)\r\n{\r\n\tif (this.label1.InvokeRequired)\r\n\t{\r\n\t\tthis.Invoke((MethodInvoker)delegate () { SafeSetLable(text); });\r\n\t\treturn;\r\n\t}\r\n\telse\r\n\t{\r\n\t\tthis.label1.Text = \"OK:\" + text + \"Sec\";\r\n\t}\r\n}<\/pre>\n<p>\u3082\u3057\u3001 \u5225\u30b9\u30ec\u30c3\u30c9\u3067\u3042\u308c\u3070(if(this.lable1.InvokeRequired))\u3001\u30e1\u30a4\u30f3\u30b9\u30ec\u30c3\u30c9\u3067\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3088\u3046\u306b\u304a\u9858\u3044(this.Invoke())\u3059\u308b\u3002<\/p>\n<p>\u3068\u3044\u3063\u305f\u5f62\u3067\u3001\u975e\u5e38\u306b\u3081\u3093\u3069\u3044\u66f8\u304d\u65b9\u3060\u3063\u305f\u3002<\/p>\n<p>\u305d\u308c\u304c\u3001\u4ee5\u4e0b\u306e\u69d8\u306b\u66f8\u3051\u308b\u3002<\/p>\n<pre class=\"lang:c# decode:true\">async private void button1_Click(object sender, EventArgs e)\r\n{\r\n\tstring text = await Task.Run(()=&gt;\r\n\t{\r\n\t\tvar start = DateTime.Now;\r\n\r\n\t\t\/\/\u4f55\u3089\u304b\u306e\u91cd\u3044\u51e6\u7406\r\n\t\tSystem.Threading.Thread.Sleep(5000);\r\n\r\n\t\tvar ts = DateTime.Now - start;\r\n\r\n\t\treturn ts.TotalSeconds.ToString();\r\n\t});\r\n\r\n\tthis.label1.Text = \"OK:\" + text + \"Sec\";\r\n}<\/pre>\n<p>async\u3000\u3068\u982d\u306b\u3064\u3044\u305f\u30e1\u30bd\u30c3\u30c9\u306e\u4e2d\u3067\u306f\u3001await \u4ee5\u4e0b\u306e\u975e\u540c\u671f\u51e6\u7406\u304c\u7d42\u308f\u308b\u307e\u3067\u5f85\u6a5f\u3057\u3066\u3001\u7d42\u308f\u3063\u305f\u3089\u30e1\u30a4\u30f3\u30b9\u30ec\u30c3\u30c9\u306b\u623b\u3063\u3066\u304d\u3066\u51e6\u7406\u3092\u7d9a\u3051\u308b\u3002<\/p>\n<p>Invoke\u3068\u304b\u66f8\u304b\u306a\u304f\u3066\u3082\u3088\u304f\u306a\u3063\u3066\u3044\u308b\u3002\u00a0 \u3044\u3084\u30fc\u3001\u4fbf\u5229\u306b\u306a\u3063\u305f\u3088\u306d\u3002<\/p>\n<p>Windows Form \u306b\u304a\u3051\u308basync await \u306f\u3001\u4ee5\u4e0b\u306e\u30b5\u30a4\u30c8\u69d8\u306e\u65b9\u304c\u8a73\u3057\u304f\u4e57\u3063\u3066\u307e\u3059\u3002<br \/>\n<a href=\"http:\/\/kimux.net\/?p=902\" target=\"_blank\">http:\/\/kimux.net\/?p=902<\/a><\/p>\n<h2>\u304a\u307e\u3051<\/h2>\n<p>\u5b9f\u969b\u306b\u30de\u30eb\u30c1\u30b9\u30ec\u30c3\u30c9\u4f5c\u3063\u3066\u3066\u30cf\u30de\u3063\u305f\u306e\u3067\u3001\u30e1\u30e2\u3002<\/p>\n<p>Task\u306f\u5185\u90e8\u3067\u30b9\u30ec\u30c3\u30c9\u30d7\u30fc\u30eb\u3068\u304b\u3046\u307e\u304f\u4f7f\u3063\u3066\u304f\u308c\u308b\u304b\u3089\u3001\u30b9\u30ec\u30c3\u30c9\u306e\u8d77\u52d5\u306b\u304b\u304b\u308b\u6642\u9593\u3092\u7121\u304f\u305b\u308b\u3068\u8003\u3048\u3066\u3044\u305f\u306e\u3067\u3059\u304c\u3001\u305d\u308c\u306f\u9593\u9055\u3044\u3067\u3001\u7d50\u5c40\u30b9\u30ec\u30c3\u30c9\u306e\u7acb\u3061\u4e0a\u304c\u308b\u6642\u9593\u306f\u3084\u3063\u3071\u308a1\u79d2\u3050\u3089\u3044\u304b\u304b\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u306a\u305c\u304b\u3068\u3044\u3046\u3068\u3001\u5f53\u305f\u308a\u524d\u3067\u3059\u304c\u30b9\u30ec\u30c3\u30c9\u30d7\u30fc\u30eb\u306f\u3001\u4f59\u3063\u3066\u3044\u308b\u30b9\u30ec\u30c3\u30c9\u306b\u4ed5\u4e8b\u3092\u3084\u3089\u305b\u308b\u306e\u3067\u3001\u4f59\u3063\u3066\u7121\u3051\u308c\u3070\u4f5c\u308b\u3057\u304b\u306a\u3044\u304b\u3089\u3067\u3059\u3002<\/p>\n<p>\u3093\u3067\u3001\u6700\u521d\u306b\u30d7\u30ed\u30b0\u30e9\u30e0\u304c\u7528\u610f\u3059\u308b\u30b9\u30ec\u30c3\u30c9\u306f\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u3060\u3068CPU\u306e\u30b3\u30a2\u6570\u3068\u540c\u3058\u306b\u306a\u308b\u3088\u3046\u3067\u30014\u30b3\u30a2\u306e\u30de\u30b7\u30f3\u3060\u3068\u3001\u4e8b\u524d\u306b4\u3064\u30b9\u30ec\u30c3\u30c9\u306f\u7528\u610f\u3055\u308c\u3066\u3044\u308b\u3051\u3069\u30015\u3064\u76ee\u306f\u3001\u524d\u306e4\u3064\u304c\u7d42\u308f\u3063\u3066\u7121\u3051\u308c\u3070\u3001\u30b9\u30ec\u30c3\u30c9\u3092\u65b0\u898f\u306b\u4f5c\u6210\u3057\u3001\u9045\u5ef6\u304c\u767a\u751f\u3059\u308b\u3068\u3044\u3046\u8a33\u3067\u3059\u3002<\/p>\n<p>\u305d\u3053\u3067\u4f7f\u3046\u306e\u304c\u4e8b\u524d\u306b\u3044\u304f\u3064\u306e\u30b9\u30ec\u30c3\u30c9\u3092\u7acb\u3061\u4e0a\u3052\u3066\u304a\u304f\u304b\u3092\u6307\u5b9a\u3059\u308b<a href=\"https:\/\/msdn.microsoft.com\/ja-jp\/library\/system.threading.threadpool.setminthreads(v=vs.110).aspx\" target=\"_blank\">SetMinThread<\/a>\u30e1\u30bd\u30c3\u30c9\u3002<\/p>\n<p>\u3069\u306e\u304f\u3089\u3044\u30b9\u30ec\u30c3\u30c9\u304c\u5fc5\u8981\u304b\u3092\u8003\u3048\u3066\u4e8b\u524d\u306b\u7acb\u3061\u4e0a\u3052\u3066\u304a\u304f\u3068\u3001\u4f5c\u6210\u306e\u6642\u9593\u306f\u7121\u304f\u306a\u308b\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002<\/p>\n<p>\u3053\u306e\u3042\u305f\u308a\u306f\u3001SetMinThread\u3067\u691c\u7d22\u3059\u308b\u3068\u8272\u3005\u3068\u5206\u304b\u308a\u3084\u3059\u3044\u8a18\u4e8b\u304c\u3042\u308b\u306e\u3067\u3001\u305d\u3061\u3089\u3092\u53c2\u8003\u306b\u3057\u3066\u3044\u305f\u3060\u3051\u308c\u3070\u3068\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C#\u306e\u30de\u30eb\u30c1\u30b9\u30ec\u30c3\u30c9(\u975e\u540c\u671f\u51e6\u7406)\u306b\u95a2\u3057\u3066\u3001\u3044\u3064\u3082\u306e\u3088\u3046\u306b\u300c\u3056\u3063\u304f\u308a\u300d\u8aac\u660e\u3002 C#\u306e\u30de\u30eb\u30c1\u30b9\u30ec\u30c3\u30c9\u306f\u3001\u4f55\u7a2e\u985e\u304b\u66f8\u304d\u65b9\u304c\u3042\u308b\u304c\u3001\u4e3b\u306b4\u3064\u306e\u30d1\u30bf\u30fc\u30f3\u304c\u3042\u308b\u3002 Thread\u3067\u30c7\u30ea\u30b2\u30fc\u30c8\u3092\u52d5\u304b\u3059\u3002 \u57fa\u672c\u3002\u3067\u3082\u3001.NetFrame&hellip;  <a href=\"https:\/\/gomokulog.gomocool.net\/?p=762\" class=\"more-link\">more <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-762","post","type-post","status-publish","format-standard","hentry","category-csharp"],"_links":{"self":[{"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=\/wp\/v2\/posts\/762","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=762"}],"version-history":[{"count":7,"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=\/wp\/v2\/posts\/762\/revisions"}],"predecessor-version":[{"id":791,"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=\/wp\/v2\/posts\/762\/revisions\/791"}],"wp:attachment":[{"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=762"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=762"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gomokulog.gomocool.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=762"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}