How to consume web service adheres to the Event-ba

2019-08-06 04:03发布

问题:

I am following the example from

http://msdn.microsoft.com/en-us/library/8wy069k1.aspx

to consume a web service implemented (by 3rd party) using the Event-based Asynchronous Pattern.

However, my program needs to do multiple calls to the DoStuffAsync() hence will get back as many DoStuffCompleted. I chose the overload which takes an extra parameter - Object userState to distinguish them.

My first question is: Is it valid to cast a GUID to Object as below, where GUID is used to generate unique taskID?

Object userState = Guid.NewGuid();

Secondly, do I need to spawn off a new thread for each DoStuffAsync() call, since I am calling it multiple times?

Also, would be nice to have some online examples or tutorials on this subject. (I've been googling for it the whole day and didn't get much back)

Many thanks

New Question: Can I bury a delegate call back in AsyncCompletedEventArgs.UserState? Just found out I need a callback to the caller to do the aftermaths...oops!

回答1:

Passing a Guid sounds fine as long as you are keeping the reference around so when the Aync method completes you know how to put it in the right context.

The Async method does the threading for you. Don't do another thread unless you want one thread that makes all your Async calls. But I'd suggest getting it working before doing anything like that. Build it up in steps.