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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.