Asynchronous Anonymous Functions

Anonymous methods using the delegate keyword or using lambda expression notification can also be marked with the async modifier. A good example of the use of this is when calling Task.Run, to start work on the task pool. A couple of overloads of this method are shown here:

public static Task Run(Action action);public static Task<TResult> Run<TResult>(Func<Task<TResult>> function);public static Task Run(Func<Task> function);public static Task<TResult> Run<TResult>(Func<TResult> function);

Additional overloads provide a CancellationToken parameter. Notice how the function and action parameter delegate types all conform to valid asynchronous method signatures. In particular, the Action ...

Get C# 5.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.