![](/img/trans.png)
[英]How do i cancel backgroundworker operation in the middle when closing the form?
[英]How do i cancel a backgroundworker operation and how to pause/continue a backgroundworker?
我有一個啟動backgroundworker的按鈕:
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
button1.Enabled = false;
button2.Enabled = true;
}
取消后台工作人員的按鈕:
private void button2_Click(object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();
button1.Enabled = true;
button2.Enabled = false;
}
后台工作dowork.progresschanged.completed事件:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bgw = (BackgroundWorker)sender;
if (bgw.CancellationPending == true)
{
return;
}
else
{
Parseanddownloadfiles();
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
lblStatus.Text = "Downloading Filename: " + e.UserState.ToString();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
stopwatch.Stop();
}
最后是方法Parseanddownloadfiles()
private void Parseanddownloadfiles()
{
using (WebClient client = new WebClient())
{
client.DownloadFile(mainurl, path_exe + "\\page.html");
}
HtmlAgilityPack.HtmlWeb hw = new HtmlAgilityPack.HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc = hw.Load(path_exe + "\\page.html");
foreach (HtmlAgilityPack.HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
{
string hrefValue = link.GetAttributeValue("href", string.Empty);
if (hrefValue.Contains("US"))
{
string url = "http://www.testing.com" + hrefValue;
parsedlinks.Add(url);
}
}
for (int i = 0; i < parsedlinks.Count; i++)
{
try
{
using (WebClient client = new WebClient())
{
string filename = parsedlinks[i].Substring(71);
client.DownloadFile(parsedlinks[i], filesdirectory + "\\" + filename);
backgroundWorker1.ReportProgress(0, filename);
}
}
catch (Exception err)
{
string error = err.ToString();
}
}
}
現在重要的是使取消生效,然后在可能后進行暫停/繼續。
更改方法int,以檢查循環內的條件為:
private void Parseanddownloadfiles()
{
using (WebClient client = new WebClient())
{
client.DownloadFile(mainurl, path_exe + "\\page.html");
}
HtmlAgilityPack.HtmlWeb hw = new HtmlAgilityPack.HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc = hw.Load(path_exe + "\\page.html");
foreach (HtmlAgilityPack.HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
{
string hrefValue = link.GetAttributeValue("href", string.Empty);
if (hrefValue.Contains("US"))
{
string url = "http://www.testing.com" + hrefValue;
parsedlinks.Add(url);
}
if (bgw.CancellationPending == true)
return;
}
for (int i = 0; i < parsedlinks.Count && bgw.CancellationPending == false; i++)
{
try
{
using (WebClient client = new WebClient())
{
string filename = parsedlinks[i].Substring(71);
client.DownloadFile(parsedlinks[i], filesdirectory + "\\" + filename);
backgroundWorker1.ReportProgress(0, filename);
}
}
catch (Exception err)
{
string error = err.ToString();
}
}
如果您需要取消下載,我最喜歡的解決方案是使用一些任務和CancellationTokenSource。
問候
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.