Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Update</h2> <p>Thinking about your problem a bit more generally, I think what you're actually trying to accomplish here is achievable in a much simpler way, without using a <code>System.Threading.Timer</code> at all.</p> <p>Is this basically what you want your method to do? Perform <code>action</code> after a specified number of milliseconds? If so, I would suggest something like the following alternative implementation instead:</p> <pre><code>private void Delay(Action action, int ms) { if (ms &lt;= 0) { action(); return; } System.Threading.WaitCallback delayed = state =&gt; { System.Threading.Thread.Sleep(ms); action(); }; System.Threading.ThreadPool.QueueUserWorkItem(delayed); } </code></pre> <p>...by the way, are you aware that in the code you posted, specifying a non-zero value for <code>ms</code> will cause <code>action</code> to be executed twice?</p> <hr> <h2>Original Answer</h2> <p>The <code>timerstate</code> class really isn't necessary. Just add a <code>System.Threading.Timer</code> member to whatever class contains your <code>Delay</code> method; then your code should look like this:</p> <pre><code>public class Delayer { private System.Threading.Timer _timer; private void Delay(Action action, Int32 ms) { if (ms &lt;= 0) { action(); } _timer = new System.Threading.Timer( (o) =&gt; action(), null, ms, System.Threading.Timeout.Infinite); } } </code></pre> <p>Now, I see that you are specifying the <code>period</code> argument of the timer's constructor as <code>System.Threading.Timeout.Infinite</code> (-1). What this means is that you intend for your timer to call <code>action</code> <strong>once</strong>, after <code>ms</code> has elapsed; am I right? If this is the case, then there's actually not much need to worry about the timer being disposed anyway (i.e., it will be, and that's fine), assuming a relatively low value for <code>ms</code>.</p> <p>Anyway, if you're going to hold onto an instance of an <code>IDisposable</code> object (like <code>System.Threading.Timer</code>), you should generally dispose of that member when <em>your</em> object (i.e., this instance) is disposed of. I believe <code>System.Threading.Timer</code> has a finalizer that will cause it to be disposed of eventually anyway, but it's best to dispose of things as soon as you don't need them anymore. So:</p> <pre><code>public class Delayer : IDisposable { // same code as above, plus... public void Dispose() { _timer.Dispose(); } } </code></pre>
    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.
 

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