{"id":661,"date":"2016-12-07T19:26:48","date_gmt":"2016-12-07T19:26:48","guid":{"rendered":"https:\/\/ibex.tech\/windows-iot\/?p=230"},"modified":"2016-12-07T19:26:48","modified_gmt":"2016-12-07T19:26:48","slug":"wait-for-async-task-to-complete","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/uwp-programming-in-c\/threads\/wait-for-async-task-to-complete","title":{"rendered":"Wait For Async Task To Complete"},"content":{"rendered":"<p>\n\tUse&nbsp;ManualResetEvent to allow you to wait on your&nbsp;thread with an anync task completes.\n<\/p>\n<h5>\n\tGlobal object<br \/>\n<\/h5>\n<pre>\n<code>\n\tprivate ManualResetEvent OurTxIsDone;\n<\/code><\/pre>\n<h5>\n\tConstructor<br \/>\n<\/h5>\n<pre>\n<code>\n\tOurTxIsDone = new ManualResetEvent(false);\n<\/code><\/pre>\n<h5>\n\tUsing It In A Method That Does Something Async Example<br \/>\n<\/h5>\n<pre>\n<code>\n\t\t\/\/Inline event handler for the Completed event (We&#039;ve implemented inline in order to make this method self-contained)\n\t\tMyOtherObjectThatsGoingToDoSomethingAsync.Completed += new EventHandler&lt;SocketAsyncEventArgs&gt;(delegate (object s, SocketAsyncEventArgs e)\n\t\t{\n\t\t\t\/\/response = e.SocketError.ToString();\t\/\/If you wanted to get any error string\n\t\t\tOurTxIsDone.Set();\t\t\t\t\t\/\/Release the original thread wait state (below)\n\t\t});\n\n\t\t\/\/Set the state of the event to nonsignaled, causing threads to block\n\t\tOurTxIsDone.Reset();\n\n\t\t\/\/Start the asynchronous Send request\n\t\tMyOtherObjectThatsGoingToDoSomethingAsync.SendToAsync(1234);\n\n\t\t\/\/WAIT on this thread for it to complete\n\t\tOurTxIsDone.WaitOne(5000);            \/\/mS timeout\n<\/code><\/pre>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Use&nbsp;ManualResetEvent to allow you to wait on your&nbsp;thread with an anync task completes. Global object private ManualResetEvent OurTxIsDone; Constructor OurTxIsDone = new ManualResetEvent(false); Using It In A Method That Does Something Async Example \/\/Inline event handler for the Completed event (We&#039;ve implemented inline in order to make this method self-contained) MyOtherObjectThatsGoingToDoSomethingAsync.Completed += new EventHandler&lt;SocketAsyncEventArgs&gt;(delegate (object [&hellip;]<\/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-661","post","type-post","status-publish","format-standard","hentry","category-threads"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/661","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=661"}],"version-history":[{"count":0,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/661\/revisions"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}