title: Run some functions parallel in an interval async?
body: <p>I have a Timer and Im calling some functions in an interval. Right now I have something like this:</p>
  
  <pre><code>private System.Timers.Timer timer;
  private int sync;
  
  void Start()
  {
    timer = new System.Timers.Timer(interval);
    timer.Elapsed += new ElapsedEventHandler(Elapsed);
    timer.Enabled = true;
  }
  
  void Elapsed(object s, ElapsedEventArgs e)
  {
    if (System.Threading.Interlocked.CompareExchange(ref sync, 1, 0) == 0)
    {
      Parallel.Invoke( () =&gt; Function1(), () =&gt; Function2(), () =&gt; Function3(),
                       () =&gt; Function4() );
    }
    sync = 0;
  }
  </code></pre>
  
  <p>This is not bad. I can start the functions parallel AND one function cannot run 2+ times, just once (this is how I want it). The problem is: lets say <code>function4()</code> takes longer then my <code>interval</code>, so the other functions have to wait too. If I remove the <code>sync</code> there, the other functions wont wait BUT another <code>function4()</code> call could start in another thread - and I dont want that one functions is running twice. Any suggestions? Thank you</p>