
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Determining Whether a Request for a Pooled Thread Will be Queued
|
1031
The CallbackAsyncDelegate method in this recipe makes use of the first parameter to
the
BeginInvoke method of the asynchronous delegate to pass in another delegate.
This contains a callback method to be called when the asynchronous delegate fin-
ishes processing. After calling
BeginInvoke, this method can now return and the
application can continue processing; it does not have to wait in a polling loop or be
blocked while the asynchronous delegate is running.
The
AsyncInvoke delegate that is passed into the first parameter of the BeginInvoke
method is defined as follows:
public delegate void AsyncCallback(IAsyncResult ar)
When this delegate is created, as shown here, the callback method passed in,
DelegateCallback, will be called as soon as the asynchronous delegate completes:
AsyncCallback callBack = new AsyncCallback(DelegateCallback);
DelegateCallback will not run on the same thread as BeginInvoke but rather on a
Thread from the ThreadPool. This callback method accepts a parameter of type
IAsyncResult. You can cast this parameter to an AsyncResult object within the
method and use it to obtain information about the completed asynchronous dele-
gate, such as its return value, any
ref parameter values, and any out parameter ...