什么是异步回调? [英] What is AsyncCallback?

查看:36
本文介绍了什么是异步回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AsyncCallback 有什么用,为什么要使用它?

What is the use of AsyncCallback and why should we use it?

推荐答案

async 方法处理完后,会自动调用AsyncCallback 方法,后处理语句可以被执行.使用此技术,无需轮询或等待 async 线程完成.

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.

这里有一些关于Async回调用法的更多解释:

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);
}

在此模型中,我们正在创建一个新的 AsyncCallback 委托,指定在操作完成时(在另一个线程上)调用的方法.此外,我们指定了一些我们可能需要作为调用状态的对象.在这个例子中,我们发送流对象是因为我们需要调用 EndRead 并关闭流.

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));
}

其他技术是等待直到完成轮询.

Wait-Until-Done 模型 wait-until-done 模型允许您启动异步调用并执行其他工作.其他工作完成后,您可以尝试结束调用,它会阻塞,直到异步调用完成.

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();

轮询模型 轮询方法类似,只是代码会轮询IAsyncResult 是否完成.

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);
}

这篇关于什么是异步回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆