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 完成后开火。RunWorkerCompleted
BeginGetResponse
BackgroundWorker
RunWorkerCompleted
EndGetResponse
这可能吗?或者我应该使用不同的异步模式?