什么是异步回调?

AsyncCallback 的用途是什么? 我们为什么要使用它?

98424 次浏览

When the async method finish the processing, AsyncCallback method is automatically called, where post processing statements can be executed. With this technique there is no need to poll or wait for the async thread to complete.

Here's some more explanation on Async Callback usage:

Callback Model: The callback model requires that we specify a method to callback on and include any state that we need in the callback method to complete the call. The callback model can be seen in the following example:

static byte[] buffer = new byte[100];


static void TestCallbackAPM()
{
string filename = System.IO.Path.Combine (System.Environment.CurrentDirectory, "mfc71.pdb");


FileStream strm = new FileStream(filename,
FileMode.Open, FileAccess.Read, FileShare.Read, 1024,
FileOptions.Asynchronous);


// Make the asynchronous call
IAsyncResult result = strm.BeginRead(buffer, 0, buffer.Length,
new AsyncCallback(CompleteRead), strm);
}

In this model, we are creating a new AsyncCallback delegate, specifying a method to call (on another thread) when the operation is complete. Additionally, we are specifying some object that we might need as the state of the call. For this example, we are sending the stream object in because we will need to call EndRead and close the stream.

The method that we create to be called at the end of the call would look something like this:

static void CompleteRead(IAsyncResult result)
{
Console.WriteLine("Read Completed");


FileStream strm = (FileStream) result.AsyncState;


// Finished, so we can call EndRead and it will return without blocking
int numBytes = strm.EndRead(result);


// Don't forget to close the stream
strm.Close();


Console.WriteLine("Read {0} Bytes", numBytes);
Console.WriteLine(BitConverter.ToString(buffer));
}

Other techniques are Wait-until-done and Polling.

Wait-Until-Done Model The wait-until-done model allows you to start the asynchronous call and perform other work. Once the other work is done, you can attempt to end the call and it will block until the asynchronous call is complete.

// Make the asynchronous call
strm.Read(buffer, 0, buffer.Length);
IAsyncResult result = strm.BeginRead(buffer, 0, buffer.Length, null, null);


// Do some work here while you wait


// Calling EndRead will block until the Async work is complete
int numBytes = strm.EndRead(result);

Or you can use wait handles.

result.AsyncWaitHandle.WaitOne();

Polling Model The polling method is similar, with the exception that the code will poll the IAsyncResult to see whether it has completed.

// Make the asynchronous call
IAsyncResult result = strm.BeginRead(buffer, 0, buffer.Length, null, null);


// Poll testing to see if complete
while (!result.IsCompleted)
{
// Do more work here if the call isn't complete
Thread.Sleep(100);
}

AsyncCallbacks are used to specify a function to call when an asynchronous operation is completed. For example, if you were doing an IO operation you would call BeginRead on a stream and pass in an AsyncCAllback delegate. The function would be called when the read operation completed.

For more information see:

Think of it this way. You have some operations that you would like to execute in parallel. You would enable this by using threads which executes asynchronously. This is a fire and forget mechanism.

But some situations call for a mechanism where you can fire and forget but need notification when the operation completes. For this, you would use an async callback.

The operation is async but calls you back when the operation completes. The advantage of this is that you don't have to wait on the operation till it completes. You are free to execute other operations and hence your thread is not blocked.

An example of this would be a background transfer of a large file. While the transfer is in progress, you don't really want to block the user from doing other operations. Once the transfer is complete, the process will call you back on an async method, where you can probably pop up a message box which says 'Transfer complete'.