title: How do I await events in C#?
body: <p>I am creating a class that has a series of events, one of them being <code>GameShuttingDown</code>. When this event is fired, I need to invoke the event handler. The point of this event is to notify users the game is shutting down and they need to save their data. The saves are awaitable, and events are not. So when the handler gets called, the game shuts down before the awaited handlers can complete.</p>
  
  <pre><code>public event EventHandler&lt;EventArgs&gt; GameShuttingDown;
  
  public virtual async Task ShutdownGame()
  {
      await this.NotifyGameShuttingDown();
  
      await this.SaveWorlds();
  
      this.NotifyGameShutDown();
  }
  
  private async Task SaveWorlds()
  {
      foreach (DefaultWorld world in this.Worlds)
      {
          await this.worldService.SaveWorld(world);
      }
  }
  
  protected virtual void NotifyGameShuttingDown()
  {
      var handler = this.GameShuttingDown;
      if (handler == null)
      {
          return;
      }
  
      handler(this, new EventArgs());
  }
  </code></pre>
  
  <h3>Event registration</h3>
  
  <pre><code>// The game gets shut down before this completes because of the nature of how events work
  DefaultGame.GameShuttingDown += async (sender, args) =&gt; await this.repo.Save(blah);
  </code></pre>
  
  <p>I understand that the signature for events are <code>void EventName</code> and so making it async is basically fire and forget. My engine makes heavy use of eventing to notify 3rd party developers (and multiple internal components) that events are taking place within the engine and letting them react to them.</p>
  
  <p>Is there a good route to go down to replace eventing with something asynchronous based that I can use? I'm not sure if I should be using <code>BeginShutdownGame</code> and <code>EndShutdownGame</code> with callbacks, but that's a pain because then only the calling source can pass a callback, and not any 3rd party stuff that plugs in to the engine, which is what I am getting with events. If the server calls <code>game.ShutdownGame()</code>, there's no way for engine plugins and or other components within the engine to pass along their callbacks, unless I wire up some kind of registration method, keeping a collection of callbacks.</p>
  
  <p>Any advice on what the preferred/recommended route to go down with this would be greatly appreciated! I have looked around and for the most part what I've seen is using the Begin/End approach which I don't think will satisfy what I'm wanting to do.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Another option I'm considering is using a registration method, that takes an awaitable callback. I iterate over all of the callbacks, grab their Task and await with a <code>WhenAll</code>.</p>
  
  <pre><code>private List&lt;Func&lt;Task&gt;&gt; ShutdownCallbacks = new List&lt;Func&lt;Task&gt;&gt;();
  
  public void RegisterShutdownCallback(Func&lt;Task&gt; callback)
  {
      this.ShutdownCallbacks.Add(callback);
  }
  
  public async Task Shutdown()
  {
      var callbackTasks = new List&lt;Task&gt;();
      foreach(var callback in this.ShutdownCallbacks)
      {
          callbackTasks.Add(callback());
      }
  
      await Task.WhenAll(callbackTasks);
  }
  </code></pre>