如何取消后台工作操作以及如何暂停/继续后台工作?

时间:2015-11-09 20:32:11

标签: c# .net winforms 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;
        }

backgroundworker dowork.progresschanged.completed events:

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();
                }
            }
        }

现在重要的是让取消工作,然后是否可以在之后暂停/继续。

1 个答案:

答案 0 :(得分:0)

更改方法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。

此致