Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should be using <code>System.Timers.Timer</code>, that will run in a seperate thread, See this <a href="http://msdn.microsoft.com/en-us/magazine/cc164015.aspx" rel="nofollow">MSDN article</a>:</p> <p>From above link:</p> <p><code>System.Windows.Forms.Timer</code></p> <blockquote> <p>If you're looking for a metronome, you've come to the wrong place. The timer events raised by this timer class are synchronous with respect to the rest of the code in your Windows Forms app. This means that application code that is executing will never be preempted by an instance of this timer class (assuming you don't call Application.DoEvents).</p> </blockquote> <p><code>System.Timers.Timer</code></p> <blockquote> <p>The .NET Framework documentation refers to the System.Timers.Timer class as a server-based timer that was designed and optimized for use in multithreaded environments. Instances of this timer class can be safely accessed from multiple threads. Unlike the System.Windows.Forms.Timer, the System.Timers.Timer class will, by default, call your timer event handler on a worker thread obtained from the common language runtime (CLR) thread pool. <strong>This means that the code inside your Elapsed event handler must conform to a golden rule of Win32 programming: an instance of a control should never be accessed from any thread other than the thread that was used to instantiate it</strong>.</p> </blockquote> <p>Working Code:</p> <pre><code>public partial class Form1 : Form { System.Timers.Timer tmr = new System.Timers.Timer(1000); volatile int timer; public Form1() { InitializeComponent(); } private void Form1_Shown(object sender, EventArgs e) { timer = 0; tmr.Elapsed += new System.Timers.ElapsedEventHandler(tmr_Elapsed); tmr.Start(); while (timer &lt; 2) ; tmr.Stop(); Console.WriteLine("timer ends"); } void tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { Console.WriteLine(timer); 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