title: The thread has exited with code 0 in Windows Forms
body: <p>I have problem with exiting threads in my Windows Forms.</p>
  
  <p>I have classic Windows Forms, which is running. I need to do something every period of time, so I added:</p>
  
  <pre><code>TimerCallback timerDelegate = new TimerCallback(this.TryDoSomething);
  int period = 10 * 1000; // to miliseconds
  System.Threading.Timer stateTimer = new System.Threading.Timer(timerDelegate, null, period, period);
  </code></pre>
  
  <p>Method DoSomething is called by few threads (main thread and this timer), so I covered it that way:</p>
  
  <pre><code>private void TryDoSomething(object o)
          {
              lock (tryDoSomethingMutex)
              {
                  if (this.dataGridView1.InvokeRequired)
                  {
                      RefreshCallback d = new RefreshCallback(DoSomething);
                      this.Invoke(d, new object[] { o });
                  }
                  else
                  {
                      this.DoSomething(o);
                  } 
              }
          }
  </code></pre>
  
  <p>And everything works good, until my timer thread just exits with message:</p>
  
  <pre><code>The thread 0x2798 has exited with code 0 (0x0).
  </code></pre>
  
  <p>Same thing happens to my FileSystemWatcher, which also calls DoSomething Method.
  Both events are independent and exit at random time (at least I did not find any rule for it)</p>
  
  <p>What causes this situation and how can I prevent it?</p>