{"id":1222,"date":"2022-10-06T17:29:44","date_gmt":"2022-10-06T16:29:44","guid":{"rendered":"https:\/\/ibex.tech\/csharp\/?p=1222"},"modified":"2022-10-06T17:48:30","modified_gmt":"2022-10-06T16:48:30","slug":"stopwatch-2","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/c-sharp\/timer-and-timing\/stopwatch-2","title":{"rendered":"Stopwatch"},"content":{"rendered":"\n<p>Stopatch is an accurate timeout timer, but it doesn&#8217;t Generate Events<\/p>\n\n\n\n<p>The stopwatch timer accurately measures elapsed time.<br>If the PC supports a high-resolution performance counter, then the Stopwatch class uses that counter to measure elapsed time. Otherwise it uses the system timer. Use the Frequency and IsHighResolution fields to determine the precision and resolution if desired.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Diagnostics;\n\n\tprivate Stopwatch Stopwatch1;\n\n\t\/\/----- START OUR HIGH ACCURACY TIMER -----\n\tStopwatch1 = new Stopwatch();\n\tStopwatch1.Reset();\n\tStopwatch1.Start();\n\tif (Stopwatch1.ElapsedMilliseconds &gt; 500)       \/\/&lt;&lt; Set mS timeout value\n\t{\n\n\t}\n\n\t\/\/If you find the timer stops on its own for some reason do this:\n\tif (!Stopwatch1.IsRunning)\n\t{\n\t\tStopwatch1.Reset();\n\t\tStopwatch1.Start();\n\t}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Creating a high priority thread that runs forever with a high accuracy stopwatch timebase<\/h4>\n\n\n\n<p><em>Note this isn\u2019t perfect!! \u00a0We\u2019ve found there will be pauses of lots time (> 200mS) every now and then,presuambly when the OS is up to something else. \u00a0<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Diagnostics;\nusing System.Threading;\n\n\tprivate const int DelayPerUpdateForMs = 20;\n\n\tprivate long StopwatchElapsedMillisecondsLast = 0;\n\tprivate int OurDelayForThisUpdateMs;\n\n\n\t\/\/----- START OUR HIGH PRIORITY BACKGROUND THREAD -----\r\n\t\/\/This thread is going to run for the lifetime of the application in the background\r\n\tStopwatch1 = Stopwatch.StartNew();\r\n\tThread newThread = new Thread(MyHighPriorityBackgroundThread);\r\n\tnewThread.Priority = ThreadPriority.Highest;\r\n\tnewThread.Start();\n\n\n\n\t\/\/**************************************************************\r\n\t\/\/**************************************************************\r\n\t\/\/********** HIGH PRIORITY BACKGROUND LOOPING THREAD ***********\r\n\t\/\/**************************************************************\r\n\t\/\/**************************************************************\r\n\tprivate void MyHighPriorityBackgroundThread()\r\n\t{\r\n\t\t\/\/This thread runs on a high priority task and loops forever\r\n\t\twhile (true)\r\n\t\t{\r\n\t\t\ttry\r\n\t\t\t{\r\n\t\t\t\t\/\/------------------------------------------\r\n\t\t\t\t\/\/----- SETUP DELAY BEFORE NEXT UPDATE -----\r\n\t\t\t\t\/\/------------------------------------------\r\n\t\t\t\tThread.Sleep(DelayPerUpdateForMs);        \/\/Delay in mS.\r\n\r\n\t\t\t\tOurDelayForThisUpdateMs = Convert.ToInt32(Stopwatch1.ElapsedMilliseconds - StopwatchElapsedMillisecondsLast);\r\n\t\t\t\tStopwatchElapsedMillisecondsLast = Stopwatch1.ElapsedMilliseconds;\r\n\t\t\t\tif (OurDelayForThisUpdateMs &lt; DelayPerUpdateForMs)\r\n\t\t\t\t\tOurDelayForThisUpdateMs = DelayPerUpdateForMs;\r\n\r\n\t\t\t\t\/\/DelayPerUpdateForMs\t\t\t\/\/&lt;&lt;&lt;How long we delay for per update\r\n\t\t\t\t\/\/OurDelayForThisUpdateMs\t\t\/\/&lt;&lt;&lt;How long we actaully delayed for this time (there can be variation to to OS delays doing other things)\r\n\r\n\t\t\t\t\/\/----- WE ARE HERE EVERY 20mS -----\r\n\r\n\r\n\n\r\n\t\t\t}\r\n\t\t\tcatch (Exception )\r\n\t\t\t{\r\n\r\n\t\t\t}\r\n\t\t} \/\/while (true)\r\n\t}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Stopatch is an accurate timeout timer, but it doesn&#8217;t Generate Events The stopwatch timer accurately measures elapsed time.If the PC supports a high-resolution performance counter, then the Stopwatch class uses that counter to measure elapsed time. Otherwise it uses the system timer. Use the Frequency and IsHighResolution fields to determine the precision and resolution if [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39],"tags":[],"class_list":["post-1222","post","type-post","status-publish","format-standard","hentry","category-timer-and-timing"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/1222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=1222"}],"version-history":[{"count":4,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/1222\/revisions"}],"predecessor-version":[{"id":1227,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/1222\/revisions\/1227"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=1222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=1222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=1222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}