{"id":681,"date":"2017-01-09T13:36:11","date_gmt":"2017-01-09T13:36:11","guid":{"rendered":"https:\/\/ibex.tech\/windows-iot\/?p=375"},"modified":"2022-09-13T14:36:04","modified_gmt":"2022-09-13T13:36:04","slug":"stopwatch","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/uwp-programming-in-c\/timer-and-timing-uwp-programming-in-c\/stopwatch","title":{"rendered":"Stopwatch"},"content":{"rendered":"<p>The stopwatch is the highest resolution timer available and goes down to 100nano-second resolution.<\/p>\n<p>The time cost of the Gpio.Write and Gpio.Read methods can be an issue with time sensitive things.<\/p>\n<h4>\n\tUsing Stopwatch To Accurately Delay<\/h4>\n<pre><code>\nusing System.Diagnostics;\n\n\tprivate&nbsp;Stopwatch Stopwatch1;\n\n\tStopwatch1 = Stopwatch.StartNew();\n\n\tStopwatchWait(5);\t\t\/\/Delay in mS\n\n\n\t\/\/*************************************\n\t\/\/*************************************\n\t\/\/*********** STOPWATCH WAIT **********\n\t\/\/*************************************\n\t\/\/*************************************\n\t\/\/A synchronous wait is used to avoid yielding the thread.  This method allows for very precise timing.\n\tprivate void StopwatchWait(double milliseconds)\n\t{\n\t\t\/\/Calculates the number of CPU ticks that will elapse in the specified time\n\t\tlong initialTick = Stopwatch1.ElapsedTicks;\n\t\tlong initialElapsed = Stopwatch1.ElapsedMilliseconds;\n\t\tdouble desiredTicks = milliseconds \/ 1000.0 * Stopwatch.Frequency;\n\t\tdouble finalTick = initialTick + desiredTicks;\n\n\t\t\/\/Wait in a loop until the threshold is hit\n\t\twhile (Stopwatch1.ElapsedTicks &lt; finalTick)\n\t\t{\n\n\t\t}\n\t}\n<\/code><\/pre>\n<pre><code>\n\tprivate void Page_Unloaded(object sender, RoutedEventArgs e)\n\t{\n\t\tStopwatch1.Stop();\n<\/code><\/pre>\n<h4>\n\tCreating a high priority thread that runs forever with a high accuracy stopwatch timebase<\/h4>\n<p><em>Note this isn&#8217;t perfect!! &nbsp;We&#8217;ve found there will be pauses of lots time (&gt; 200mS) every now and then,presuambly when the OS is up to something else. &nbsp;<\/em><\/p>\n<pre><code>\nusing System.Diagnostics;\n\n\tStopwatch Stopwatch1;\n\n\t\/\/----- START OUR HIGH PRIORITY BACKGROUND THREAD -----\n\t\/\/We don't await it as its going to run for the lifetime of the application in the background\n\tStopwatch1 = Stopwatch.StartNew();\n\tWindows.System.Threading.ThreadPool.RunAsync(this.MyHighPriorityBackgroundThread, Windows.System.Threading.WorkItemPriority.High);\n\n\n\t\/\/******************************************************\n\t\/\/******************************************************\n\t\/\/********** HIGH PRIORITY BACKGROUND THREAD ***********\n\t\/\/******************************************************\n\t\/\/******************************************************\n\tprivate void MyHighPriorityBackgroundThread(Windows.Foundation.IAsyncAction action)\n\t{\n\t\t\/\/This thread runs on a high priority task and loops forever\n\t\twhile (true)\n\t\t{\n\t\t\t\/\/DO SOMETHING...\n\n\n\t\t\t\/\/Delay using high accuracy timer\n\t\t\tStopwatchWait(5);\n\t\t\t\t\n\t\t}\n\t}\n\n\n\t\/\/*************************************\n\t\/\/*************************************\n\t\/\/*********** STOPWATCH WAIT **********\n\t\/\/*************************************\n\t\/\/*************************************\n\t\/\/A synchronous wait is used to avoid yielding the thread.  This method allows for very precise timing.\n\tprivate void StopwatchWait(double milliseconds)\n\t{\n\t\t\/\/Calculates the number of CPU ticks that will elapse in the specified time\n\t\tlong initialTick = Stopwatch1.ElapsedTicks;\n\t\tlong initialElapsed = Stopwatch1.ElapsedMilliseconds;\n\t\tdouble desiredTicks = milliseconds \/ 1000.0 * Stopwatch.Frequency;\n\t\tdouble finalTick = initialTick + desiredTicks;\n\n\t\t\/\/Wait in a loop until the threshold is hit\n\t\twhile (Stopwatch1.ElapsedTicks &lt; finalTick)\n\t\t{\n\n\t\t}\n\t}\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The stopwatch is the highest resolution timer available and goes down to 100nano-second resolution. The time cost of the Gpio.Write and Gpio.Read methods can be an issue with time sensitive things. Using Stopwatch To Accurately Delay using System.Diagnostics; private&nbsp;Stopwatch Stopwatch1; Stopwatch1 = Stopwatch.StartNew(); StopwatchWait(5); \/\/Delay in mS \/\/************************************* \/\/************************************* \/\/*********** STOPWATCH WAIT ********** \/\/************************************* \/\/************************************* [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[],"class_list":["post-681","post","type-post","status-publish","format-standard","hentry","category-timer-and-timing-uwp-programming-in-c"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/681","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=681"}],"version-history":[{"count":1,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/681\/revisions"}],"predecessor-version":[{"id":1151,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/681\/revisions\/1151"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}