不要异步Web服务调用总是调用的AsyncCallback? [英] Do asynchronous web service calls always call the AsyncCallback?

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

问题描述

我正在从C#应用程序异步Web服务调用:

I'm making asynchronous web service calls from a C# app:

{
  //Put UI in 'loading' state
  ...

  //Now call web service
  webServiceProxy.BeginMyMethod(param, new AsyncCallback(MyCallback), null);
}


private void MyCallback(IAsyncResult res)
{
   ...
   //process result

   // Put UI back in normal state (yes I'm marshalling back to the UI thread)
}

在主线程使应用程序在等待模式,然后在回调函数的末尾重新启用控件。我看到,偶尔,用户界面​​永远停留在加载模式的错误。

The main thread puts the app in a "waiting" mode, and then the end of the Callback function re-enables the controls. I'm seeing a bug that occasionally, the UI is stuck forever in the loading mode.

现在有可能只是在回调code错误(有相当多的出现),但我的问题在这里的社区是这样的:

Now there may just be a bug in the callback code (there's quite a bit there), but my question to the community here is this:

时的myCallBack函数保证被调用? presuming说,BeginMyMethod没有抛出异常,我可以肯定的是myCallBack函数将被执行?我看到一个CompletedSynchronously和由的BeginXXX函数返回的IAsyncResult的IsCompleted,但我不知道这是重要与否。

Is "MyCallback" GUARANTEED to be called? Presuming that "BeginMyMethod" didn't throw an exception, can I be sure that MyCallback will be executed? I'm seeing a "CompletedSynchronously" and "IsCompleted" on the IAsyncResult returned by the BeginXXX functions, but I'm not sure if that's important or not.

推荐答案

是的,回调是保证被调用。回调是允许使用异步code中的开始* / 结束* 模式写在延续-passing风格。

Yes, the callback is guaranteed to be called. The callback is what permits asynchronous code using the Begin* / End* pattern to be written in a continuation-passing style.

您必须调用相应的结束* 在回调方法(通常情况下,在回调的第一件事情),但是。它是如何在异步方法的信号,可能在呼叫期间已发生异常,有一点,以及方式得到的结果输出(如果有的话)。

You must call the corresponding End* method in your callback (normally, the first thing in the callback), however. It is how the asynchronous method signals an exception that may have occurred during the call, for one thing, as well as the way to get a result out (if any).

使用C#2.0,当编码异步模式使用匿名委托有时更优雅,并允许写入呼叫后延续接近呼叫的启动,以及允许更容易的数据通过捕获变量共享,提供了使用适当的同步措施。

Coding the asynchronous pattern using anonymous delegates when using C# 2.0 is sometimes more elegant, and permits writing of the post-call continuation close to the initiation of the call, as well as permitting much easier data sharing through captured variables, providing that appropriate synchronization measures are used.

参考: http://msdn.microsoft.com/en-us/库/ ms228972.aspx

应用程序,可以做其他的工作在等待异步操作的结果不应该阻塞等待,直到操作完成。使用下列选项之一,以继续执行指令,等待异步操作完成:

Applications that can do other work while waiting for the results of an asynchronous operation should not block waiting until the operation completes. Use one of the following options to continue executing instructions while waiting for an asynchronous operation to complete:

      
  • 使用一个AsyncCallback委托来处理异步操作的结果,在一个单独的线程。这种方法证明了这个主题研究。
  •   

[...]

这篇关于不要异步Web服务调用总是调用的AsyncCallback?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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