How to consume web service adheres to the Event-based Asynchronous Pattern?

人盡茶涼 提交于 2019-12-13 18:46:38

问题


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.



来源:https://stackoverflow.com/questions/2607038/how-to-consume-web-service-adheres-to-the-event-based-asynchronous-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!