3

I need to be able to cancel async calls made to my webservice. One solution I have is to use manage my own threads, and use synchronous methods from the SOAP client. This works fine and it needs some more fine grained thread management.

If I used any of these two patterns provided from adding a web service reference, say:

var Client = new ASL_WS.SvcSoapClient()
IAsyncResult result = Client.BeginAuthenticateUser(Email, Password, new AsyncCallback(AuthCompleted));

or

var Client = new ASL_WS.SvcSoapClient()
Client.AuthenticateUserCompleted += AuthCompleted;
Client.AuthenticateUserAsync(Email, Passsword);

do any of these two patterns give me a way of cancelling the request? One use case could be: a user logs in, but wants to cancel before the authenticate call completes.

Of course, I could implement this differently by modifying the asyncState passed to these calls, and setting it to disable UI update, but it's not what I'm looking for.

Could I just just cancel all outstanding operations. Does Client.Abort() cancel such operations. What if there are many async requests, are all cancelled? Are there any other API methods that can do this?

Candide
  • 30,469
  • 8
  • 53
  • 60

1 Answers1

7

Yes, you can use Abort method but keep below notes in mind. You can also use CancelAsync.

Abort notes: http://msdn.microsoft.com/en-us/library/aa480512.aspx

When you call the Abort method, any outstanding requests will still complete, but they will complete with a fault. This means that if you are using callbacks, your callback function will still be called for each outstanding request . When the EndInvoke method is called, or in our case, the wrapper function EndDelayedResponse, then a fault will be generated indicating that the underlying connection has been closed.

CancelAsync example: http://www.codeproject.com/KB/cpp/wsasync.aspx

Priyank
  • 10,503
  • 2
  • 27
  • 25
  • 1
    Makes sense. It took me a while to figure out what the code was doing, but it is pretty obvious now. I ended up doing something similar. Thanks. – Candide Oct 07 '11 at 17:56