The AsyncDelegates Sample

Links: Up

This sample shows how to create and execute asynchronous F# code, to be managed by the .NET Thread Pool. The code is in main.fs.

NOTE: The sample requires .NET 2.0 (Whidbey) since it uses a generic delegate type.

The call to BeginInvoke schedules an asynchronous work item with the thread pool, which will get executed at some later point. The sample illustrates the two ways to specify what happens when the computation terminates, either by explicitly calling EndInvoke on the IAsyncResult operation that acts as a handle to the asynchronous computation, or (often better) by giving an AsyncCallback item to the BeginInvoke operation - the AsyncCallback will get invoked when the operation completes.

You may wish to look at the online MSDN documentation on CreateDelegate and AsyncCallback when reading the code.

Suggested Exercises