{"id":478,"date":"2011-08-23T09:40:33","date_gmt":"2011-08-23T07:40:33","guid":{"rendered":"http:\/\/hen.co.za\/blog\/?p=478"},"modified":"2011-08-23T09:40:33","modified_gmt":"2011-08-23T07:40:33","slug":"waiting-for-your-exit","status":"publish","type":"post","link":"https:\/\/hen.co.za\/blog\/2011\/08\/waiting-for-your-exit\/","title":{"rendered":"Waiting for your exit"},"content":{"rendered":"<p>With a heading like this the post could be mistaken for something more philosophical but fortunately this is a plain developmental posting \ud83d\ude42<\/p>\n<p>Suppose you have some program that does some work and then have to &#8216;sleep&#8217; for a while before doing some more work &#8211; like a service checking or polling stuff. You can simply use something like a Thread.Sleep() but that also means the thread cannot do or respond to anything else during that time &#8211; like the program or service stopping. That would mean the stopping process has to wait until the &#8216;sleeping&#8217; thread becomes active again.<\/p>\n<p>A solution to this is by having a variable or property that indicates if the program or service is still running and having the &#8216;waiting&#8217; routine wait in little &#8216;jumps&#8217; checking the &#8216;IsRunning&#8217; variable each time and then waiting a little bit more until the full &#8216;sleeping&#8217; period time is done.<\/p>\n<blockquote>\n<pre><span style=\"color: #0000ff;\">private\u00a0void<\/span>\u00a0BackgroundWaitIsPolling(<span style=\"color: #0000ff;\">int\u00a0<\/span>nextWaitInterval)\r\n{\r\n \u00a0 <span style=\"color: #0000ff;\">int\u00a0<\/span>waitTimeRemaining;\r\n \u00a0 <span style=\"color: #0000ff;\">int\u00a0<\/span>decrementBy\u00a0=\u00a02000;\r\n \u00a0 <span style=\"color: #0000ff;\">if\u00a0<\/span>(IsRunning)\r\n\u00a0\u00a0 {\r\n \u00a0 \u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">try<\/span>\r\n \u00a0 \u00a0\u00a0\u00a0\u00a0{\r\n \u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: #0000ff;\">if<\/span>\u00a0((nextWaitInterval\u00a0&lt;=\u00a0decrementBy)\u00a0&amp;&amp;\u00a0(nextWaitInterval\u00a0&gt;\u00a00))\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n  \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Thread.Sleep(nextWaitInterval);\r\n\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: #0000ff;\">else<\/span>\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n \u00a0             waitTimeRemaining\u00a0=\u00a0nextWaitInterval;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0  <span style=\"color: #0000ff;\">while\u00a0<\/span>(IsRunning &amp;&amp;\u00a0(waitTimeRemaining\u00a0&gt;\u00a00))\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0 <span style=\"color: #0000ff;\">if\u00a0<\/span>(waitTimeRemaining\u00a0&lt;=\u00a0decrementBy)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0  {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0    waitTimeRemaining\u00a0=\u00a00;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: #0000ff;\">else<\/span>\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 waitTimeRemaining\u00a0-=\u00a0decrementBy;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: #0000ff;\">if\u00a0<\/span>(decrementBy\u00a0&gt;\u00a00)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Thread.Sleep(decrementBy);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0  }\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <span style=\"color: #0000ff;\">catch\u00a0<\/span>{\u00a0}\r\n \u00a0 }\r\n}<\/pre>\n<\/blockquote>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With a heading like this the post could be mistaken for something more philosophical but fortunately this is a plain developmental posting \ud83d\ude42 Suppose you have some program that does some work and then have to &#8216;sleep&#8217; for a while &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/hen.co.za\/blog\/2011\/08\/waiting-for-your-exit\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[7,8,9,5,303,24],"class_list":["post-478","post","type-post","status-publish","format-standard","hentry","category-development","tag-net","tag-c","tag-code","tag-code-sharing","tag-development","tag-tip"],"_links":{"self":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/comments?post=478"}],"version-history":[{"count":3,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/478\/revisions"}],"predecessor-version":[{"id":481,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/478\/revisions\/481"}],"wp:attachment":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/media?parent=478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/categories?post=478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/tags?post=478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}