Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You say this is a webservice - timers are not guaranteed to work correctly in a web service as the lifetime of the owning threads is not guaranteed. Consider using a global worker thread that is created when the app domain starts up.</p> <p>A basic global worker thread is:</p> <pre><code>static bool _stopPolling = false; static Thread _pollerThread; static object _oneTimeLocker = new object(); private static CreatePollerThread() { if(_pollerThread == null) { lock(_oneTimeLocker) { if(_pollerThread == null) //double-check { _pollerThread = new Thread( new ThreadStart(() =&gt; { while(true &amp;&amp; !_stopPolling) { DoWork(); Thread.Sleep(10000); } })); _pollerThread.Start(); } } } } </code></pre> <p>I've used a double-checked lock there in case you want to fire up the global thread in response to a request (not best policy - since you can then have multiple requests trying to intialise the thread, potentially doing it multiple times). This isn't the only way - but it's rolling off my tongue at the moment given that I'm at work and don't have a lot of time!</p> <p>An important factor here is the _stopPolling boolean that you use to shut the thread down in a friendly manner.</p> <p>If you're in Asp.Net - then this SO: <a href="https://stackoverflow.com/questions/4617613/application-end-and-background-processes-exiting-asp-net-application-gracefully">Application_End and background processes, exiting ASP.Net application gracefully</a>, with answer by Aristos, will help you with how best to shut the thread down.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload