如何使用基于事件的异步模式的Web服务?

发布于 2024-08-29 00:13:54 字数 758 浏览 2 评论 0原文

中的示例

我正在遵循http://msdn.microsoft.com/en- us/library/8wy069k1.aspx

来使用(由第 3 方)使用基于事件的异步模式实现的 Web 服务。

但是,我的程序需要多次调用 DoStuffAsync(),因此将返回尽可能多的 DoStuffCompleted。我选择了带有额外参数的重载 - Object userState 来区分它们。

我的第一个问题是: 如下所示将 GUID 转换为对象是否有效,其中 GUID 用于生成唯一的任务ID?

Object userState = Guid.NewGuid();

其次,我是否需要为每个 DoStuffAsync() 调用生成一个新线程,因为我多次调用它?

另外,如果有一些关于这个主题的在线示例或教程,那就太好了。 (我一整天都在谷歌上搜索它,但没有得到太多回报)

非常感谢

新问题:我可以在 AsyncCompletedEventArgs.UserState 中埋入委托回调吗? 刚刚发现我需要回调呼叫者来完成善后工作...哎呀!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恰似旧人归 2024-09-05 00:13:54

只要您保留引用,传递 Guid 听起来就很好,因此当 Aync 方法完成时,您知道如何将其放在正确的上下文中。

Async 方法为您执行线程处理。除非您想要一个线程进行所有异步调用,否则不要执行另一线程。但我建议在做类似的事情之前让它发挥作用。逐步构建它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文