如何知道异步函数何时完成c#

如何知道异步函数何时完成c#,c#,asynchronous,webclient,C#,Asynchronous,Webclient,我从网上下载了很多URL,但我不知道什么时候所有的URL都下载完毕。与此同时,我写了一些东西,但它似乎太我一个垃圾,虽然它的工作 在我的代码中,我从dtPPdataTable下载所有URL,并将它们保存到matUrlFharsing数组中 这是我的密码: main() { Parallel.For(0, dtPP.Rows.Count, i => { string f = ""; WebClient client = new WebCli

我从网上下载了很多URL,但我不知道什么时候所有的URL都下载完毕。与此同时,我写了一些东西,但它似乎太我一个垃圾,虽然它的工作

在我的代码中,我从
dtPP
dataTable下载所有URL,并将它们保存到
matUrlFharsing
数组中

这是我的密码:

main()
{
    Parallel.For(0, dtPP.Rows.Count, i =>
    {
         string f = "";
         WebClient client = new WebClient();
         client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);
         lock (dtPP.Rows[i])
         {
              f = dtPP.Rows[i]["pp_url"].ToString();
         }
         client.DownloadDataAsync(new Uri(f), i);
    });
    while (end)
    {
    }
    DoSomething();
 }


void client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
        {
            int h = (int)e.UserState;
            page = (int)e.UserState;
            myString = enc.GetString(e.Result);

            lock (matUrlFharsing[h])
            {
                lock (dtPP.Rows[h])
                {
                    //save in mat
                    matUrlFharsing[h] = Pharsing.CreateCorrectHtmlDoc(myString);
                }
            }

            lock (myLocker)
            {
                ezer = false;
                for (int j = 0; j < matUrlFharsing.Length && !ezer; j++)
                {
                    if (matUrlFharsing[j] == "")
                        ezer = true;
                }
                end = ezer;
            }
        }
main()
{
Parallel.For(0,dtPP.Rows.Count,i=>
{
字符串f=“”;
WebClient客户端=新的WebClient();
client.DownloadDataCompleted+=新的DownloadDataCompletedEventHandler(client\u DownloadDataCompleted);
锁(dtPP.Rows[i])
{
f=dtPP.Rows[i][“pp_url”].ToString();
}
DownloadDataAsync(新Uri(f),i);
});
(完)
{
}
DoSomething();
}
无效客户端\u DownloadDataCompleted(对象发送方,DownloadDataCompletedEventArgs e)
{
inth=(int)e.UserState;
page=(int)e.UserState;
myString=enc.GetString(例如,Result);
锁(matUrlFharsing[h])
{
锁(dtPP.Rows[h])
{
//保存在垫子中
matUrlFharsing[h]=Pharsing.CreateCorrectHtmlDoc(myString);
}
}
锁(myLocker)
{
以西=假;
对于(int j=0;j
我能做些什么来改进或改变它?

小改进:

使用而不是bool作为结束

AutoResetEvent autoResetEvent;

main()
{
    autoResetEvent = new AutoResetEvent(false);
    Parallel.For(0, dtPP.Rows.Count, i =>
    {
        // your code
    });
    autoResetEvent.WaitOne();
    DoSomething();
 }

void client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
   ...
   if(ezer)
   {
       autoResetEvent.Set();
   }

}

异步函数完成后,将调用completed事件。你在正确的道路上

您可以在最后改进代码。这个While语句将运行得非常快。你可以:

  • 您可以使用睡眠1s来减少CPU处理
  • 您可以在while中设置超时,以确保应用程序未被锁定

您可以使用AutoResetEvent数组,将它们作为参数发送到事件接收器,然后等待它们全部完成

AutoResetEvent[] autoResetEvents;
main()
{
    autoResetEvent = new AutoResetEvent[dtPP.Rows.Count];
    Parallel.For(0, dtPP.Rows.Count, i =>
    {
         string f = "";
         WebClient client = new WebClient();
         client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);
         lock (dtPP.Rows[i])
         {
              f = dtPP.Rows[i]["pp_url"].ToString();
         }
         Object[] parameters = new Object[2];
         autoResetEvents[i] = new AutoResetEvent(false);
         parameters[0] = i;
         parameters[1] = autoResetEvent[i];

         client.DownloadDataAsync(new Uri(f), parameters);
    });
    WaitHandle.WaitAll(autoResetEvents);
    DoSomething();
 }

    void client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
    {
        Object[] parameters = (object[])e.UserState;
        autoResetEvent = (AutoResetEvent)parameters[1];
        int h = (int)parameters[0];
        page = (int)e.UserState;
        myString = enc.GetString(e.Result);

        lock (matUrlFharsing[h])
        {
            lock (dtPP.Rows[h])
            {
                //save in mat
                matUrlFharsing[h] = Pharsing.CreateCorrectHtmlDoc(myString);
            }
        }
        autoResetEvent.Set();
    }

与第一个答案的主要区别是什么?在我的第一个答案中,我建议您手动检查是否下载了所有数据,然后向主线程发送信号。在这里,我建议每次下载都使用AutoResetEvent,并在主线程中等待来自所有下载的信号,检查每次下载的所有数据是否都不好,所以这个答案更好。