2010-04-13 54 views
2

我有一个我正在使用的API,它的文档有限。我被告知它执行的一些方法是异步调用的。C#从异步调用中获取结果

如何获得这些异步调用的结果。请注意,我没有做任何特殊的调用,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(); 
    } 
+0

有.NET中不同的模式做异步调用。你能提供一些示例代码吗? – 2010-04-13 15:27:38

+1

发表一些代码。通常,异步API调用将委托作为参数 - 只要您调用的任何对象完成,就会调用该委托。委托方法是您的程序中您可以获得“回复”的地方。 – MusiGenesis 2010-04-13 15:29:04

+0

如果API提供了异步方法,那么它必须提供方法/事件来获取这些异步方法的结果,或者自己封装这些方法。 – Bobby 2010-04-13 15:29:24

回答

6

有一些操作可以在.NET中发生异步的几种方法。既然你没有张贴任何细节,我给比较常见的模式概述:

AsyncWaitHandle

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

调用开始函数返回一个AsyncWaitHandle对象,它可用于像同步线程常规WaitHandle(通过调用WaitOne或者将它传递到静态WaitHandle.WaitAnyWaitHandle.WaitAll函数),然后被传递到相应的“结束”功能获取函数的返回值(或者,如果发生了,则抛出遇到的异常)。

如果您使用简单的.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); 

通常异步方法加上事件,如下图所示。

活动

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

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

样品使用AsyncWaitHandle和事件

(代码中的某处)

object.OperationComplete += object_OperationComplete; 

(其他地方)

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

这些一般工作在各种不同的方式...您可以保留自己从Begin操作返回的IAsyncResult,但大多数库会通过临时t IAsyncResult对象作为其特定EventArgs类中的属性。

找到作为EventArgs类的属性的返回值也很常见。如果Begin操作返回IAsyncResult,则使用该值时正确无误,,即使您需要的数据位于EventArgs中,调用相应的End函数总是一个好主意。 End函数通常也是捕获异常的方式。

回调

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

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

实施例使用回调

object.BeginOperation(args, OperationComplete); 

... 

void OperationComplete(SomeObject results) 
{ 
    ... 
} 
0

通过将指针传递给回调函数,可以从异步调用中获得结果。请让我们知道您尝试拨打哪个API,并且有人可能会为您提供一些示例代码。

2

通常情况下,您会传入一个函数的引用,该方法在完成时将“回调”。有关此行为的一个很好的示例,请查看BackgroundWorker及其RunWorkCompleted事件。这被设置为一个属性而不是作为参数传入,但同样的想法适用。

但是,如果您有一些源代码,或者至少是您使用的API的名称,我们可能会更具体。

这里的回调方法作为参数传递的一个基本的例子:

public SearchForm : Form 
{ 
    public void StartSearch(string searchtext) 
    { 
     searcher.SearchAsync(searchtext, SearchCompleted); 
    } 

    public void SearchCompleted(IList<SearchResult> results) 
    { 
     // Called by the async searcher component, possibly on another thread 

     // Consider marshalling to the UI thread here if you plan to update the UI like this: 
     if (InvokeRequired) 
     { 
      Invoke(new Action<IList<SearchResult>>(SearchCompleted), results); 
      return; 
     } 

     foreach (var result in Results) 
     { 
      AddToList(result); 
     } 
    } 
} 
+0

我认为这个答案是正确的。我认为关键在于评论代码。我将如何能够将它编组为与UI – 2010-04-13 16:37:00

+0

相同的线程,我修改了示例以假定它在Form上工作,因此调用了Form.InvokeRequired和Form.Invoke。 – 2010-04-13 22:42:10

1

有几种成语用于传送异步结果。基于你的问题,我最好的猜测是你正在处理接受委托他们完成调用的方法(“回调”)。

您必须定义匹配委托类型并将其传递给异步方法的方法。完成后,它会调用委托,通常通过一个或多个委托参数传递结果。

举例来说,如果你有这样的异步方法:

public class Foo { 
    public static void DoSomethingAsynchronous(SimpleCallback simpleCallback) { 

     // First this method does something asynchronous 

     // Then it calls the provided delegate, passing the operation's results 
     simpleCallback(true); 
    } 
} 

// The result parameter can be much richer - it's your primary mechanism for 
// passing results back to the caller. 
public delegate SimpleCallback(bool result); 

你会这样称呼它:

public static void Main(string[] args) { 
    Foo.DoSomethingAsynchronous(WriteResultToConsole); 
} 

// Here's your SimpleCallback 
public static WriteResultToConsole(bool result) { 
    Console.WriteLine(result? "Success!" : "Failed!"); 
} 
+0

有趣。如果没有回调并使用事件过程呢? – 2010-04-13 16:35:53