Fire and Forget approach

It depends on the semantics you want. If you want to ensure exceptions are noticed, then yes, you could await the task. But in that case it’s not truly “fire and forget”.

A true “fire and forget” – in the sense that you don’t care about when it completes or whether it completes successfully or with error – is extremely rare.

Edit:

For handling exceptions:

public static async void Forget(this Task task, params Type[] acceptableExceptions)
{
  try
  {
    await task.ConfigureAwait(false);
  }
  catch (Exception ex)
  {
    // TODO: consider whether derived types are also acceptable.
    if (!acceptableExceptions.Contains(ex.GetType()))
      throw;
  }
}

Note that I recommend using await instead of ContinueWith. ContinueWith has a surprising default scheduler (as noted on my blog) and Task.Exception will wrap the actual exception in an AggregateException, making the error handling code more cumbersome.

Leave a Comment