我可以使用委托的单个实例来启动多个异步请求? [英] Can I use a single instance of a delegate to start multiple Asynchronous Requests?

查看:192
本文介绍了我可以使用委托的单个实例来启动多个异步请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道,如果有人可以澄清一些委托的一个实例使用的BeginInvoke ,当你想使多个异步调用,因为MSDN文档并没有真正涵盖/提到这一点的。

Just wondered if someone could clarify the use of BeginInvoke on an instance of some delegate when you want to make multiple asynchronous calls since the MSDN documentation doesn't really cover/mention this at all.

我想要做的就是类似如下:

What I want to do is something like the following:

MyDelegate d = new MyDelegate(this.TargetMethod);
List<IAsyncResult> results = new List<IAsyncResult>();

//Start multiple asynchronous calls
for (int i = 0; i < 4; i++)
{
   results.Add(d.BeginInvoke(someParams, null, null));
}

//Wait for all my calls to finish
WaitHandle.WaitAll(results.Select(r => r.AsyncWaitHandle).ToArray());

//Process the Results

现在的问题是,我可以做到这一点与委托的一个实例还是需要委托为每个单独的呼叫实例?

The question is can I do this with one instance of the delegate or do I need an instance of the delegate for each individual call?

由于 EndInvoke会()需要一个的IAsyncResult 作为参数,我会认为前者是正确的,但我什么都看不到的文件中表示两种方式。

Given that EndInvoke() takes an IAsyncResult as a parameter I would assume that the former is correct but I can't see anything in the documentation to indicate either way.

推荐答案

是的,没有问题。你会得到不同的IAsyncResult每次调用的BeginInvoke()。有没有与该委托启动线程关联的状态对象本身。

Yes, no problem. You'll get a different IAsyncResult for each call to BeginInvoke(). There's no state associated with the started thread in the delegate object itself.

这篇关于我可以使用委托的单个实例来启动多个异步请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆