Search code examples
c#nito.asyncex

Correct usage for Nito.AsyncEx.AsyncContext.Run


I am using the excellent library from Steven called AsyncEx to help me baby-step the transition of an old codebases across to the async world.

The docs say to use a delegate:

class Program
{
  static async Task<int> AsyncMain()
  {
    ..
  }

  static int Main(string[] args)
  {
    return AsyncContext.Run(AsyncMain);
  }
}

When calling a method with a lambda expression, should I be doing:

var blah = AsyncContext.Run(() => MyMethodAsync(myvar));

or specify the async/wait in the call?

var blah = AsyncContext.Run( async () => await MyMethodAsync(myvar));

Solution

  • I now believe that this is the same as Return Task or Await Questions and therefore Rene's answer there will be correct

    poorly summarized here as -- bit more overhead in await for the compiler, but basically the same at runtime.