{"id":4297,"date":"2014-03-30T09:44:27","date_gmt":"2014-03-30T09:44:27","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/async-pattern-in-a-top-level-worker-method-collection-of-common-programming-errors\/"},"modified":"2014-03-30T09:44:27","modified_gmt":"2014-03-30T09:44:27","slug":"async-pattern-in-a-top-level-worker-method-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/async-pattern-in-a-top-level-worker-method-collection-of-common-programming-errors\/","title":{"rendered":"async pattern in a top-level worker method-Collection of common programming errors"},"content":{"rendered":"<p>We have some worker threads in a web role. It is created in the OnStart method<\/p>\n<pre><code>Task.Factory.StartNew(() =&gt; DoWorkAsync(), TaskCreationOptions.LongRunning);\n<\/code><\/pre>\n<p>Then the DoWorkAsync method will continue polling the queue and do some time consuming work. The actual work takes several minutes as it submits a query job and waits for the result to come back. Those functions are in async pattern. What&#8217;s the right way to implement the top-level DoWorkAsync method? If I do the following, then it&#8217;s kind of blocking here, and only one PollingQueueAndWorkAsync() is called.<\/p>\n<pre><code>public async void DoWorkAsync()\n{\n  while (_continue)\n  {\n    await PollingQueueAndWorkAsync();\n  }\n}\n<\/code><\/pre>\n<p>Or the following? It doesn&#8217;t look right as it keeps running the method in a different thread.<\/p>\n<pre><code>public async void DoWorkAsync()\n{\n  while (_continue)\n  {\n    Task.Run(() =&gt; PollingQueueAndWorkAsync());\n  }\n}\n<\/code><\/pre>\n<p>I was trying the refactor the code to be &#8220;async all the way&#8221;. But how should it end in the top-level worker method?<\/p>\n<p><strong>[Update]<\/strong><\/p>\n<p>After trying more, here is what I have:<\/p>\n<pre><code>public async void DoWorkAsync()\n{\n  while (_continue)\n  {\n    Task.Run(async () =&gt; await PollingQueueAndWorkAsync());\n  }\n}\n<\/code><\/pre>\n<p>The behavior is what I want, since new messages keep adding to the queue, this loop can process as many as messages in the queue. However after running this for a while, it throws our of memory exception. Is it because it creates too many continuations? So I should use SemaphoreSlim to throttle number of tasks?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We have some worker threads in a web role. It is created in the OnStart method Task.Factory.StartNew(() =&gt; DoWorkAsync(), TaskCreationOptions.LongRunning); Then the DoWorkAsync method will continue polling the queue and do some time consuming work. The actual work takes several minutes as it submits a query job and waits for the result to come back. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4297","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4297","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4297"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4297\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}