Do asynchronous web service calls always call the

2020-07-30 04:12发布

问题:

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.

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:

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.

回答1:

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).

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.

Ref: http://msdn.microsoft.com/en-us/library/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:

  • Use an AsyncCallback delegate to process the results of the asynchronous operation in a separate thread. This approach is demonstrated in this topic.

[...]



回答2:

The AsyncCallback will be called regardless of whether the operation was completed synchronously or asynchronously.



回答3:

I believe the callback is not guaranteed to be called if the CompletedSynchronously property is true.