{"id":7519,"date":"2015-11-06T00:12:03","date_gmt":"2015-11-05T06:12:03","guid":{"rendered":"http:\/\/www.moonmile.net\/blog\/?p=7519"},"modified":"2015-11-06T00:14:34","modified_gmt":"2015-11-05T15:14:34","slug":"c-%e3%83%9f%e3%83%aa%e7%a7%92%e5%8d%98%e4%bd%8d%e3%81%ae%e3%82%bf%e3%82%a4%e3%83%9e%e3%83%bc%e3%82%92%e4%bd%9c%e6%88%90%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/www.moonmile.net\/blog\/archives\/7519","title":{"rendered":"[C#] \u30df\u30ea\u79d2\u5358\u4f4d\u306e\u30bf\u30a4\u30de\u30fc\u3092\u4f5c\u6210\u3059\u308b"},"content":{"rendered":"<p>MonoBrick \u3092\u4f7f\u3044\u5012\u7acb\u632f\u5b50\u30ed\u30dc\u30c3\u30c8\u3092 C# \u306b\u79fb\u690d\u3057\u305f\u3068\u3053\u308d\u3067\u3059\u304c\u3001\u30b8\u30e3\u30a4\u30ed\u3084\u30b5\u30fc\u30dc\u306e\u56de\u8ee2\u6570\u306e\u30b5\u30f3\u30d7\u30ea\u30f3\u30b0\u30ec\u30fc\u30c8\u306b\u554f\u984c\u304c\u3042\u308a\u307e\u3059\u3002\u3082\u3068\u3082\u3068\u306e\u30b3\u30fc\u30c9\u304c\u3060\u3044\u305f\u3044 20 msec \u5358\u4f4d\uff081\u79d2\u9593\u306b50\u56de\uff09\u3050\u3089\u3044\u306e\u30b5\u30f3\u30d7\u30ea\u30f3\u30b0\u3092\u884c\u3063\u3066\u3044\u308b\u3082\u306e\u306e\u3001\u9014\u4e2d\u3067 C# \u3067 Timer \u30af\u30e9\u30b9\u3092\u4f7f\u3046\u3068 30 msec \u3050\u3089\u3044\u306e\u7cbe\u5ea6\u3057\u304b\u306a\u3044\u3093\u3067\u3059\u3088\u306d\u3002\u3057\u304b\u3082\u3001\u3060\u3093\u3060\u3093\u3068\u5024\u304c\u305a\u308c\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<p>\u3061\u3087\u3063\u3068\u8003\u3048\u3066\u3001\u304d\u3061\u3093\u3068 20 msec \u5358\u4f4d\u3067\u30b5\u30f3\u30d7\u30ea\u30f3\u30b0\u3067\u304d\u308b TickTimer \u30af\u30e9\u30b9\u3092\u4f5c\u3063\u305f\u306e\u3067\u516c\u958b\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<h2>TickTimer \u30af\u30e9\u30b9<\/h2>\n<p>System.Diagnostics.Stopwatch \u3068 Thread \u3092\u4f7f\u3063\u3066\u6b63\u78ba\u306b msec \u5358\u4f4d\u306e\u5272\u308a\u8fbc\u307f\u3092\u767a\u751f\u3055\u305b\u307e\u3059\u3002\u4f7f\u3044\u65b9\u306f\u3001Timer \u30af\u30e9\u30b9\u3068\u540c\u3058\u3088\u3046\u306b\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u3092\u6307\u5b9a\u3057\u3066\u4f7f\u3044\u307e\u3059\u3002<br \/>\n\u6700\u521d\u306f Task \u3067\u4f5c\u3063\u305f\u306e\u3067\u3059\u304c\u3001mono \u306e Task \u751f\u6210\u304c\u9045\u3044\u3089\u3057\u304f Thread \u306b\u5207\u308a\u66ff\u3048\u3066\u3044\u307e\u3059\u3002MonoBrick \u304c .NET 4.0 \u30d9\u30fc\u30b9\u306a\u306e\u3067 async\/await \u304c\u4f7f\u3048\u306a\u3044\u3057\u3001\u307e\u3042 Task \u3067\u3042\u308b\u5fc5\u8981\u3082\u306a\u3044\u306e\u3067\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class TickTimer\r\n{\r\n\tTimerCallback _cb;\r\n\tStopwatch _sw;\r\n\tint _dueTime;\r\n\tint _period;\r\n\tbool _loop = true;\r\n\tThread _task;\r\n\r\n\tpublic TickTimer(TimerCallback callback, object state, int dueTime, int period)\r\n\t{\r\n\t\t_cb = callback;\r\n\t\t_dueTime = dueTime;\r\n\t\t_period = period;\r\n\t\t_sw = new Stopwatch();\r\n\t\t_task = new Thread(onTimer);\r\n\t\t_task.Start( state );\r\n\t}\r\n\tpublic TickTimer(TimerCallback callback, int period)\r\n\t{\r\n\t\t_cb = callback;\r\n\t\t_period = period;\r\n\t\t_sw = new Stopwatch();\r\n\t\t_task = new Thread(onTimer);\r\n\t}\r\n\tpublic void Start( object state = null )\r\n\t{\r\n\t\t_dueTime = 0;\r\n\t\t_task.Start( state );\r\n\t}\r\n\r\n\tpublic void Stop()\r\n\t{\r\n\t\t_loop = false;\r\n\t}\r\n\tprivate void onTimer(object state)\r\n\t{\r\n\t\tThread.Sleep(_dueTime);\r\n\t\t_sw.Restart();\r\n\t\twhile (_loop)\r\n\t\t{\r\n\t\t\tlong msec = _sw.ElapsedMilliseconds;\r\n\t\t\tint rest = _period - (int)(msec % _period);\r\n\t\t\t\/\/ 200msec\u3060\u3051\u4f59\u3089\u305b\u3066\u30b9\u30ea\u30fc\u30d7\r\n\t\t\tif (rest &gt; 200)\r\n\t\t\t{\r\n\t\t\t\tThread.Sleep(rest - 200);\r\n\t\t\t}\r\n\t\t\t\/\/ 200msec\u306e\u9593\u3001\u3061\u3087\u3046\u3069\u306b\u306a\u308b\u307e\u3067\u30eb\u30fc\u30d7\u3067\u5f85\u3064\r\n\t\t\twhile (true)\r\n\t\t\t{\r\n\t\t\t\tif (_sw.ElapsedMilliseconds &gt;= msec + rest)\r\n\t\t\t\t{\r\n\t\t\t\t\tbreak;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tif (_cb != null)\r\n\t\t\t{\r\n\t\t\t\t_cb(state);\r\n\t\t\t}\r\n\t\t}\r\n\t\t_sw.Stop();\r\n\t}\r\n}\r\n<\/pre>\n<h2>\u5229\u7528\u30b3\u30fc\u30c9<\/h2>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nclass Program\r\n{\r\n\tstatic void Main(string&#x5B;] args)\r\n\t{\r\n\t\t\/\/ 1\u79d2\u6bce\u306b\u30bf\u30a4\u30de\u30fc\u3092\u767a\u751f\u3055\u305b\u308b\r\n\t\t\/\/ System.Threading.Timer \u3067\u9593\u9694 20msec \u3092\u6307\u5b9a\u3059\u308b\u3068 30msec \u7a0b\u5ea6\u306b\u306a\u308b.\r\n\t\t\/\/ 1.0sec \u3092\u6307\u5b9a\u3057\u3066\u3082\u5b8c\u5168\u306b1.0\u306b\u306f\u306a\u3089\u306a\u3044\u30021msec\u3050\u3089\u3044\u305a\u308c\u3066\u3044\u304f\r\n\t\tTimer tm = new Timer(timerCB,null,0,1000);\r\n\t\tConsole.WriteLine(&quot;Timer start.&quot;);\r\n\t\tConsole.ReadKey();\r\n\t\ttm.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);\r\n\r\n\t\t\/\/ System.Diagnostics.Stopwatch \u3092\u5229\u7528\u3057\u3066\u3001\u6b63\u78ba\u306a\u30bf\u30a4\u30de\u30fc\u3092\u4f5c\u308b \r\n\t\tTickTimer tm2 = new TickTimer(timerCB, null, 0, 1000);\r\n\t\tConsole.WriteLine(&quot;TickTimer start.&quot;);\r\n\t\tConsole.ReadKey();\r\n\t\ttm2.Stop();\r\n\t\t\/\/ 20msec\u3082\u6b63\u78ba\u306b\u6e2c\u308c\u308b\r\n\t\t\/\/ TickTimer tm3 = new TickTimer(timerCB, null, 0, 20);\r\n\t\tTickTimer tm3 = new TickTimer(timerCB, 20);\r\n\t\ttm3.Start();\r\n\t\tConsole.WriteLine(&quot;TickTimer start at 20msec.&quot;);\r\n\t\tConsole.ReadKey();\r\n\t\ttm3.Stop();\r\n\t}\r\n\tstatic void timerCB(object obj)\r\n\t{\r\n\t\tvar msec = DateTime.Now.Millisecond;\r\n\t\tConsole.WriteLine(&quot;msec: {0}&quot;, msec);\r\n\t}\r\n}\r\n<\/pre>\n<p>\u5b9f\u884c\u3059\u308b\u3068\u3001\u3053\u3093\u306a\u611f\u3058\u3002<\/p>\n<p><a href=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2015\/11\/wpid-dworkblogimage20151106_01org.jpg\"><img decoding=\"async\" src=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2015\/11\/wpid-dworkblogimage20151106_01thum.jpg\" border=\"0\" \/><\/a><\/p>\n<p>Timer \u30af\u30e9\u30b9\u3092\u4f7f\u3046\u3068 1msec \u305a\u3064\u305a\u308c\u3066\u3044\u304f\u3051\u3069\u3001TickTimer \u306e\u5834\u5408\u306f\u30011msec \u7a0b\u5ea6\u305a\u308c\u3066\u3082\u5143\u306b\u623b\u308b\u306e\u3067\u6b63\u78ba\u306a 1000msec \u3084 20msec \u3067\u5272\u308a\u8fbc\u307f\u3092\u5165\u308c\u3089\u308c\u307e\u3059\u3002<\/p>\n<p>\u3053\u306e\u5272\u308a\u8fbc\u307f\u3092\u4f7f\u3063\u3066\u3001PID \u5236\u5fa1\u306e\u7a4d\u5206\u6210\u5206\u3092\u51fa\u305b\u3070\u826f\u3044\u306f\u305a\u3067\u3001\u3053\u308c\u3067\u5e73\u5747\u5024\u3092\u51fa\u3059\u3068\u304d\u306e\u305a\u308c\u304c\u6e1b\u308b\u304b\u306a\u3068\u3002\u3044\u307e\u306e\u3068\u3053\u308d\u3001\u3053\u3093\u306a\u611f\u3058\u3067\u5012\u7acb\u3067\u304d\u3066\u3044\u307e\u3059\u3002<\/p>\n<p><a title=\"20151106_01\" href=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2015\/11\/20151106_01.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2015\/11\/20151106_01.png\" alt=\"20151106_01\" width=\"484\" height=\"648\" class=\"alignnone size-full wp-image-7522\" srcset=\"https:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2015\/11\/20151106_01.png 484w, https:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2015\/11\/20151106_01-224x300.png 224w\" sizes=\"auto, (max-width: 484px) 100vw, 484px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>MonoBrick \u3092\u4f7f\u3044\u5012\u7acb\u632f\u5b50\u30ed\u30dc\u30c3\u30c8\u3092 C# \u306b\u79fb\u690d\u3057\u305f\u3068\u3053\u308d\u3067\u3059\u304c\u3001\u30b8\u30e3\u30a4\u30ed\u3084\u30b5\u30fc\u30dc\u306e\u56de\u8ee2\u6570\u306e\u30b5\u30f3\u30d7\u30ea\u30f3\u30b0\u30ec\u30fc\u30c8\u306b\u554f\u984c\u304c\u3042\u308a\u307e\u3059\u3002\u3082\u3068\u3082\u3068\u306e\u30b3\u30fc\u30c9\u304c\u3060\u3044\u305f\u3044 20 msec \u5358\u4f4d\uff081\u79d2\u9593\u306b50\u56de\uff09\u3050\u3089\u3044\u306e\u30b5\u30f3\u30d7\u30ea &hellip; <a href=\"https:\/\/www.moonmile.net\/blog\/archives\/7519\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,23],"tags":[],"class_list":["post-7519","post","type-post","status-publish","format-standard","hentry","category-dev","category-csharp"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/7519","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/comments?post=7519"}],"version-history":[{"count":3,"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/7519\/revisions"}],"predecessor-version":[{"id":7523,"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/7519\/revisions\/7523"}],"wp:attachment":[{"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/media?parent=7519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/categories?post=7519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/tags?post=7519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}