Asynchronous Call Programming Models

To support asynchronous invocation, multiple threads are required. However, it would be a waste of system resources and a performance penalty if .NET spun off a new thread for every asynchronous method invocation. A better approach is to use a pool of already created worker threads. .NET has just such a pool, called the .NET thread pool . One of the nice things about the .NET way of supporting asynchronous calls is that it hides this interaction completely. As previously indicated, there are quite a few programming models available for dealing with asynchronous calls. This section examines the various options: blocking, waiting, polling, and completion callbacks. In general, BeginInvoke() initiates an asynchronous method invocation. The calling client is blocked for only the briefest moment—the time it takes to queue up a request for a thread from the thread pool to execute the method—and then control returns to the client. EndInvoke() manages method completion; specifically, retrieving output parameters and return values, and error handling.

Using BeginInvoke() and EndInvoke()

The compiler-generated BeginInvoke() and EndInvoke() methods take this form:

    public virtual IAsyncResult BeginInvoke(<input and input/output parameters>,
                                            AsyncCallback callback,
                                            object asyncState);
    public virtual <return value> EndInvoke(<output and input/output parameters>,
                                            IAsyncResult asyncResult);

BeginInvoke() accepts the input parameters of the original signature the ...

Get Programming .NET Components, 2nd Edition 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.