Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>EDIT: My original answer is rubbish. Really rubbish. I've kept it here to explain <em>why</em> it's rubbish though - it's in the comments, but they'd have been deleted with the answer.</p> <p>GC.KeepAlive only makes sure a reference is treated as a root until after the call. In the code at the bottom of this answer, the GC.KeepAlive method would be called immediately, and then the timer would still be eligible for garbage collection. Because the newly created thread is a foreground thread, the app will run as long as it's alive (whereas the timer uses a background thread, which doesn't prevent program exit). This means that the Main method exits, but the application needs to keep running.</p> <p>Arguably a <em>simpler</em> solution would be to run <code>myThreadStart</code> in the main thread, rather than creating a new one and then letting the main thread die. In other words, a simple solution would be:</p> <pre><code>using System.Threading; class Program { static void Main() { Timer timer = new Timer(myTimerCallback, new MyStateObject(), 0, 5000); myThreadStart(); GC.KeepAlive(timer); } } </code></pre> <p>I assume the <em>real</em> code is more complicated though - in which case using a private static variable as suggested in other answers is probably the way to go. It really will depend on the usage though. I personally prefer not to create a static field just for the sake of preventing something being collected if there's an alternative (like the above) but sometimes it's virtually the only way of doing it.</p> <p><strong>Original (bad) answer:</strong></p> <p>If you really want to allocate it in Main, then you can use <a href="http://msdn.microsoft.com/en-us/library/system.gc.keepalive.aspx" rel="noreferrer">GC.KeepAlive</a>:</p> <pre><code>using System.Threading; class Program { static void Main() { new Thread(myThreadStart).Start(); Timer timer = new Timer(myTimerCallback, new MyStateObject(), 0, 5000); GC.KeepAlive(timer); } } </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.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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