title: Parallel loops running at different intervals
body: <p>In C# I am trying to create a Windows Service with two loops that will run simultaneously at set intervals. For example, LoopA will run every 30 seconds and LoopB every 5 seconds. </p>
  
  <p>The tasks that will take place inside LoopA could take more than 30 secodns to complete. If this happens, LoopA will begin again immediately, where as if the task only takes 20 seconds, LoopA will not run again for 10 seconds.</p>
  
  <p>I have already implemented LoopA. My problem now is implementing LoopB to run in parallel, behaving in the same way as LoopA except with a 5 second interval. Here is my LoopA code, which runs inside a Thread:</p>
  
  <pre><code>private Thread Worker;
  private AutoResetEvent StopRequest;
  
  private TimeSpan loop_a_freq;
  private TimeSpan loob_b_freq;
  
  public LoopManager(Thread _worker, AutoResetEvent _stopRequest)
  {
      // Threads
      this.Worker = _worker;
      this.StopRequest = _stopRequest;
  
      RunLoopA();
  }
  
  private void RunLoopA()
  {
      DateTime start_current_loop = DateTime.Now;
      DateTime next_loop = start_current_loop + loop_a_freq;
  
      for (;;)
      {
          // Do processing here
          new LoopAction();
  
          DateTime now = DateTime.Now;
          if (now &gt; next_loop)
          {
              next_loop = now;
          }
  
          TimeSpan delay = next_loop - now;
          Thread.Sleep(delay);
  
          next_loop += loop_a_freq;
  
          if (StopRequest.WaitOne(0))
          {
              return;
          }
      }
  }
  </code></pre>
  
  <p>How do I go about implementing <code>RunLoopB()</code>? Threads are the one aspect of C# I have never explored fully. Do I need to use <code>Parallel.For()</code>?</p>