C# 使用委托的异步调用

C# 使用委托的异步调用,c#,delegates,asynchronous,C#,Delegates,Asynchronous,我目前正在使用委托在for循环中进行异步调用,我遇到的问题是-如何知道这些异步调用何时完成 例如: public delegate string GetMergeSectionCaller(string something1, out int threadId); public Dataset GetDataset (param1, param2) { int threadId; Dataset d

我目前正在使用委托在for循环中进行异步调用,我遇到的问题是-如何知道这些异步调用何时完成

例如:

         public delegate string GetMergeSectionCaller(string something1, out int threadId);

         public Dataset GetDataset (param1, param2) {
               int threadId;
               Dataset ds = new Dataset;

               using (myConnection) {
                   myConnection.StartConnection();
                   GetMergeSectionCaller caller = new GetMergeSectionCaller(GetMergeSection);
                   foreach (var r in anObjectList) {
                       IAsyncResult result = caller.BeginInvoke(r.ToString(), out threadId, null, null);
                   }

                   //I want to do here is wait for the above every single foreach BeginInvoke to finish; then do the below job
                   ds = GetFinalData();
               }
               //do more thing to ds here;
               return ds;
            }

         public void GetMergeSectionCaller(string something1, out int threadId) {
             //doing superlong long job
             //in my actual case, it's actually inserting data to db etc

             Thread.Sleep(5000);
             threadId = Thread.CurrentThread.ManagedThreadId;
         }
所以我尝试了不同的方法;例如,将回调传递给我的BeginInvoke和EndInvoke,但仍然——在我完成foreach之前,我缺少停止其余代码运行的方法


也许我错过了一些很简单的东西?。。。。有人能根据我的情况给我看一个完整的工作样品吗

您有几个选择。IAsyncResult有一个属性,可用于等待

var results = new List<WaitHandle>();
foreach (var r in anObjectList) {
   results.Add(caller.BeginInvoke(r.ToString(), out threadId, null, null).WaitHandle);
}
// wait for all results to complete
WaitHandle.WaitAll(results.ToArray());
更新2

下面介绍如何使用线程池中的工作线程和ManualResetEvent运行任务

ManualResetEvent mreComplete = new ManualResetEvent(false);
int callsRemaining;

GetMergeSectionCaller caller = new GetMergeSectionCaller(GetMergeSection);
callsRemaining = anObjectList.Count;
mreComplete.Reset();
foreach (var r in anObjectList) {
    ThreadPool.QueueUserWorkItem((Action)delegate {
        caller(r.ToString());
        lock{
           if(--callsRemaining==0) mreComplete.Set();
        }
    }
}
mreComplete.Wait();

提到这是哪种编程语言会有所帮助。只需编辑标题:in c#woot woot!!非常感谢!!实际上,我尝试了你提到的所有方法,但不知道我可以像列表中那样处理waitHandle。我的测试和工作都很棒!!另一方面,我想知道您是否也能告诉我如何使用ManualResetEvent?在跳到stackoverflow寻求帮助之前,我试着用这口井,但没有运气;是的!我正在使用Parallel.ForEach进行测试,如果我在这里没有得到任何帮助,我将万不得已,再次感谢!!!我将添加一个ManualResetEvent与TreadPool组合的示例。
ManualResetEvent mreComplete = new ManualResetEvent(false);
int callsRemaining;

GetMergeSectionCaller caller = new GetMergeSectionCaller(GetMergeSection);
callsRemaining = anObjectList.Count;
mreComplete.Reset();
foreach (var r in anObjectList) {
    ThreadPool.QueueUserWorkItem((Action)delegate {
        caller(r.ToString());
        lock{
           if(--callsRemaining==0) mreComplete.Set();
        }
    }
}
mreComplete.Wait();