{"id":628,"date":"2016-11-18T13:37:23","date_gmt":"2016-11-18T13:37:23","guid":{"rendered":"https:\/\/ibex.tech\/windows-iot\/?p=87"},"modified":"2022-10-07T18:14:05","modified_gmt":"2022-10-07T17:14:05","slug":"run-a-task-on-a-background-thread","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/uwp-programming-in-c\/threads\/run-a-task-on-a-background-thread","title":{"rendered":"Run a task on a background thread"},"content":{"rendered":"\n<p>You can&#8217;t create a background thread for a&nbsp;task, but you can schedule it via a static RunAsync method on the Windows.System.Threading.ThreadPool class.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Create a high priority thread that runs forever<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Diagnostics;\n\n\n\t\/\/----- START OUR HIGH PRIORITY BACKGROUND THREAD -----\n\t\/\/We don't await it as its goign to run for the lifetime of the application in the background\n\tWindows.System.Threading.ThreadPool.RunAsync(this.MyHighPriorityBackgroundThread, Windows.System.Threading.WorkItemPriority.High);\n\n\n\t\t\/\/******************************************************\n\t\t\/\/******************************************************\n\t\t\/\/********** HIGH PRIORITY BACKGROUND THREAD ***********\n\t\t\/\/******************************************************\n\t\t\/\/******************************************************\n\t\tprivate void MyHighPriorityBackgroundThread(Windows.Foundation.IAsyncAction action)\n\t\t{\n\t\t\t\/\/This thread runs on a high priority task and loops forever\n\t\t\twhile (true)\n\t\t\t{\n\t\t\t\t\/\/DO SOMETHING...\n\n\n\t\t\t\t\n\t\t\t}\n\t\t}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You can&#8217;t create a background thread for a&nbsp;task, but you can schedule it via a static RunAsync method on the Windows.System.Threading.ThreadPool class. Create a high priority thread that runs forever<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69],"tags":[],"class_list":["post-628","post","type-post","status-publish","format-standard","hentry","category-threads"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/628","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=628"}],"version-history":[{"count":1,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/628\/revisions"}],"predecessor-version":[{"id":1243,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/628\/revisions\/1243"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}