Note that there are some explanatory texts on larger screens.

plurals
  1. POC# separate thread that calls a method on a timer (Timer out of scope)
    primarykey
    data
    text
    <p>I have a working version of my program, where the main part of my program runs on the same thread as the GUI using a forms.timer.</p> <p>I am trying to upgrade to a better version that uses 2 threads. One of my threads (that processes all of the information) needs to be called repeatedly (on a timer). </p> <p>I am using the following from my main class: </p> <pre><code>Timer tmr = new Timer(new TimerCallback(bot.refresh), null, 0, 1000); </code></pre> <p>Where bot.refresh is the method I want to call every second (1000ms) until I stop it.</p> <pre><code>public void refresh(Object obj) { Debug.WriteLine("Updated: " + DateTime.Now.ToString()); } </code></pre> <p>About 1/3rd of the time the timer will run stop and I will get this output:</p> <pre><code>Updated: 23/11/2013 4:37:24 PM Updated: 23/11/2013 4:37:25 PM Updated: 23/11/2013 4:37:26 PM Updated: 23/11/2013 4:37:27 PM Updated: 23/11/2013 4:37:28 PM Updated: 23/11/2013 4:37:29 PM Updated: 23/11/2013 4:37:30 PM The thread '&lt;No Name&gt;' (0x3f20) has exited with code 0 (0x0). The thread '&lt;No Name&gt;' (0x37a0) has exited with code 0 (0x0). </code></pre> <p>Any suggestions for why this could be happening?</p> <p>Also, <strong>is there a better way I can do this?</strong> I have the feeling that this is an odd way to do it.</p> <p>Thanks for your help.</p> <p>EDIT: </p> <p>Will this go out of scope? This way I can start the timer when I need to rather than when Main is constructed. For it to be referenced does it need to be in the constructor?</p> <pre><code>class Main { Timer tmr; public Main() { } public void start() { tmr = new Timer (new TimerCallback(bot.refresh), null, 0, 1000); } } </code></pre> <p>Thanks.</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.
 

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