title: Is it okay to attach async event handler to System.Timers.Timer?
body: <p>I have already read the SO posts <a href="https://stackoverflow.com/questions/36661338/how-to-call-an-async-method-from-within-elapsedeventhandler">here</a> and article <a href="http://theburningmonk.com/2012/10/c-beware-of-async-void-in-your-code/" rel="noreferrer">here</a>. I have a timer event that fires every once in a while and I want to do some asynchronous processing inside the handler, so something along the lines of:</p>
  <pre class="lang-cs prettyprint-override"><code>Timer timer = new Timer();
  timer.Interval = 1000;
  timer.Elapsed += timer_Elapsed; // Please ignore this line. But some answers already given based on this line so I will leave it as it is.
  timer.Elapsed += async (sender, arguments) =&gt; await timer_Elapsed(sender, arguments);
  
  private async Task timer_Elapsed(object sender, ElapsedEventArgs e)
  {
      await Task.Delay(10);
  }
  </code></pre>
  <p>The code above is compiling and working.</p>
  <p>But I am not sure why the code is compiling. The <code>ElapsedEventHandler</code> expected signature is</p>
  <pre class="lang-cs prettyprint-override"><code>void timer_Elapsed(object sender, ElapsedEventArgs e)
  </code></pre>
  <p>However my method returns <code>Task</code> instead of <code>void</code> since <code>async void</code> is not recommended. but that does not match with <code>ElapsedEventHandler</code> signature and yet it's still compiling and working?</p>
  <p>Is it okay to call an async method on <code>Timer.Elapsed</code>? The code will be executed inside a Windows service.</p>
  <p><strong>Update 1</strong></p>
  <blockquote>
  <p>async void is &quot;not recommended&quot;, with one very important exception:
  event handlers.</p>
  </blockquote>
  <p>Does it matter if it's an asynchronous event handler or synchronous event hander?</p>
  <p>An MSDN article <a href="https://docs.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming" rel="noreferrer">here</a> says:</p>
  <blockquote>
  <p>Void-returning async methods have a specific purpose: to make
  asynchronous event handlers possible.</p>
  </blockquote>
  <p><code>Timer.Elapsed</code> is I think a synchronous event handler; can I still attach <code>async void</code> to it?</p>