title: Calling a method after set amount of time and/or aborting thread issues
body: <p>So I've got an application that employs a filesystemWatcher and triggers an event just fine. The FSW will trigger a bunch of times pretty close together. I want to create a function that triggers say an hour after the last time the FSW was triggered. </p>
  
  <p>I first tried using a backgroundworker: (All code is shortened for clarity)</p>
  
  <pre><code>namespace Devo
  {
      public partial class Form1 : Form
      {
          BackgroundWorker bw = new BackgroundWorker();
  
          private void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
          {
  
              if (bw.IsBusy)
              {
                  bw.CancelAsync(); //this is to, in a way, reset the timer for the delayed method.
              }
  
              //do a lot of stuff
  
              bw.RunWorkerAsync();
          }
  
          private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
          {
              Stopwatch sw = new Stopwatch();
              sw.Start();
  
              while(sw.ElapsedMilliseconds &lt; 180000)
              {
                  if (bw.CancellationPending == true)
                  {
                      sw.Stop();
                      sw.Reset();
                      e.Cancel = true;
                      return;
                  }
              }
  
              sw.Stop();
              sw.Reset();
  
              DelayedMethod();
          }
      }
  }
  </code></pre>
  
  <p>This didn't work as the second time <code>bw.RunWorkerAsync()</code> was called it was apparently busy, even though the call to <code>bw.CancelAsync()</code>.</p>
  
  <p>My next attempt involved a regular thread as I read somewhere on SO (can't find the link now) that one could not "restart" a backgroundWorker as I am trying to do.</p>
  
  <p>The thread attemp is nearly identical but I thought I'd try in since there might be some constraints within the backgroundWorker that is not present in a regular thread. I thought.</p>
  
  <pre><code>namespace Devo
  {
      public partial class Form1 : Form
      {
          Thread PWC_counter_thread = new Thread(PWC_Counter);
  
          private void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
          {
  
              if (PWC_counter_thread.IsAlive)
                  PWC_counter_thread.Abort();
  
              //do a lot of stuff
  
              PWC_counter_thread.Start();
          }
  
          static void PWC_Counter()
          {
              Thread.Sleep(180000);
  
              DelayedMethod();
          }
      }
  }
  </code></pre>
  
  <p>But this gave me the same error. On the second call to <code>PWC_counter_thread.Start()</code> is was busy. </p>
  
  <p>I'm assuming that a race condition is <strong>not</strong> present as the second thread waits for, in this example, 3 minutes, and the initial FSW method takes a good full second to execute, therefore I believe that the call to .Abort() and .CancelAsync() both are done before their respective methods are completed.</p>
  
  <p>Now for the questions:</p>
  
  <p>Is it possible to restart a thread in the fashion I am trying? If so, what am I doing wrong?</p>
  
  <p>Should I delay my method call in another way? If so, tips?</p>
  
  <p><strong>EDIT/UPDATE/SOLUTION</strong></p>
  
  <p>I never got starting and stopping a thread to work as I wanted so I found another solution to my situation.</p>
  
  <p>The situation was that I had a second thread that worked as a sort of timer where it would call a method after a set amount of time. My first thread did some work and upon finishing it would start the second thread. If the first thread got fired up again before the timer-thread had finished it was supposed to kill the thread and restart it.
  This proved, for me, to be difficult to get the way I wanted. So I instead took another approach towards my wanted end result. Instead of restarting the thread I simply restarted the stopwatch that my second thread was using as a counter. This gave me the result I wanted. It's probably bad practice but it works. </p>