1

BackgoundWorker我有用于处理昂贵操作的UI 代码。内部DoWork是通过 向远程 Web 服务器发送多个 Web 请求的方法BeginGetResponse

UI部分是:

 private void start_Click(object sender, RoutedEventArgs e)
    {
        BackgroundWorker bw = new BackgroundWorker();
        bw.WorkerReportsProgress = true;
        if (bw.IsBusy != true)
        {
            bw.DoWork += new DoWorkEventHandler(Processing.FormWebRequests);
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Processing.FinishBw);
            bw.RunWorkerAsync();
        }
    }

DoWork执行以下:

public static void  FormWebRequests(object sender, DoWorkEventArgs e)
    {
        foreach (webSites websiteitem in websites)
        {
            //Creation of webRequest object

            webRequest.BeginGetResponse(new AsyncCallback(FinishWebRequest), webRequest);

        }
    }

最后,回调方法:

private static void FinishWebRequest(IAsyncResult result)
{
    using (HttpWebResponse response = wr.EndGetResponse(result) as HttpWebResponse)
        {
           //Parsing httpResponse
        }
}

显然,在最后一次发送后立即BackgroundWorker触发。问题是我想在 last 完成后开火。RunWorkerCompletedBeginGetResponseBackgroundWorkerRunWorkerCompletedEndGetResponse

这可能吗?或者我应该使用不同的异步模式?

4

1 回答 1

0

BeginGetResponse已经是异步的。此方法立即返回。当响应完成时, FinishWebRequest将被解雇。所以,你不需要使用BackgroundWorker.

这是事件驱动的。

于 2014-05-24T04:08:36.740 回答