C#从异步调用中获取结果

Unk*_*der 2 c# asynchronous

我有一个我正在使用的API,它的文档有限.有人告诉我,它执行的一些方法是异步调用的.

如何获得这些异步调用的结果.请注意,我没有做任何特殊的事情来调用它们,API处理异步部分.但我似乎无法从这些调用中得到"回复" - 我假设那是因为他们在另一个线程中.

更新 - 我已经包含了下面的一些代码.API使用事件过程进行回调,但它似乎永远不会触发.

public partial class Window1 : Window
{
    ClientAppServer newServer= new ClientAppServer();

    public Window1()
    {
        InitializeComponent();
        newServer.ReceiveRequest += ReadServerReply;
    }


    private void ReadServerReply(RemoteRequest rr)
    {

        MessageBox.Show("reading server reply");
        if ((rr.TransferObject) is Gateways)
        {
            MessageBox.Show("you have gateways!");
        }
    }


    private void login()
    {
            newServer.RetrieveCollection(typeof(Gateways), true);

    }


    private void button1_Click(object sender, RoutedEventArgs e)
    {
        this.login();
    }
Run Code Online (Sandbox Code Playgroud)

Ada*_*son 6

有几种方法可以在.NET中异步进行操作.由于您没有发布任何细节,我将概述更常见的模式:

AsyncWaitHandle

通常你会发现(特别是在.NET框架类)的名为方法对BeginEnd(即像BeginReceiveEndReceive).

调用begin函数会返回一个AsyncWaitHandle对象,该对象可用于同步线程(如WaitHandle通过调用WaitOne或将其传递给静态函数WaitHandle.WaitAnyWaitHandle.WaitAll函数),然后传递到相应的"end"函数以获取函数的返回值(或者,如果发生了一次,则抛出遇到的异常).

如果您使用简单的.NET工具异步调用方法(为函数创建委托并调用BeginInvokeEndInvoke),那么这就是您需要使用的方法.但是,大多数内置支持异步操作的API将为您处理实际的异步调用部分,为您提供正确的命名BeginXXXEndXXX函数,而不是强迫您自己创建委托.

样品使用WaitHandle:

IAsyncResult result = object.BeginOperation(args);

result.AsyncWaitHandle.WaitOne(); 
// that will cause the current thread to block until
// the operation completes. Obviously this isn't
// exactly how you'd ordinarily use an async method
// (since you're essentially forcing it to be
// synchronous this way)

returnValue = object.EndOperation(result);
Run Code Online (Sandbox Code Playgroud)

异步方法通常与事件耦合,如下所示.

活动

有时,异步完成的方法调用将在完成时引发事件.这有时也与AsyncWaitHandle提供流程已完成的通知的方法结合使用.例如,MSMQ库在队列对象上有一个BeginReceiveEndReceieve函数,以及一个ReceieveCompleted事件.当事件触发时,它表示EndReceive可以调用,传入AsyncWaitHandle从调用返回的对象BeginReceive.获取收到的实际消息.

无论哪种方式,您都会附加到事件并像使用任何其他事件一样使用它.

样品使用AsyncWaitHandle和事件:

(代码中的某个地方)

object.OperationComplete += object_OperationComplete;
Run Code Online (Sandbox Code Playgroud)

(别处)

IAsyncResult result = object.BeginOperation(args);

// at this point, you can either wait for the event
// to fire, or use the WaitHandle approach as shown
// in the previous example

...

void objectOperationComplete(object sender, AsyncOperationEventArgs e)
{
    returnValue = object.EndOperation(e.AsyncResult);
}
Run Code Online (Sandbox Code Playgroud)

这些通常以各种方式工作...您可以自己保留IAsyncResult从Begin操作返回的内容,但是大多数库将该IAsyncResult对象作为其特定EventArgs类的属性传递.

找到返回值作为EventArgs类的属性也很常见.虽然使用这个值很好,但是如果Begin操作返回了一个IAsyncResult,那么调用相应的End函数总是一个好主意,即使你需要的数据在EventArgs.End函数通常也是捕获异常的方式.

回调

回调(在.NET中)是提供给异步函数的委托.回调不仅仅用于异步函数,但在这种情况下,它们通常是一个委托,它在调用它完成时将调用的函数时提供.

回调类似于事件(因为它们都是基于委托的),尽管方法调用和提供的回调之间存在更多的一对一关联.

使用回调的示例:

object.BeginOperation(args, OperationComplete);

...

void OperationComplete(SomeObject results)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)