0

在 Form1 中,我删除/删除了 _busy 变量。在 Form1 顶部我做了:

BackgroundWebCrawling bgwc;

然后在button4暂停点击事件中我做了:

private void button4_Click(object sender, EventArgs e)
{
    bgwc.PauseWorker();
    label6.Text = "Process Paused";
    button5.Enabled = true;
    button4.Enabled = false;
}

在 button5 点击事件按钮中我做了:

private void button5_Click(object sender, EventArgs e)
{
    bgwc.ContinueWorker();
    label6.Text = "Process Resumed";
    button4.Enabled = true;
    button5.Enabled = false;
}

和取消按钮点击事件:

private void button3_Click(object sender, EventArgs e)
{
    bgwc.CancelWorker();
    cancel = true;
}

然后,如果取消为真,我将检查 Form1 完成事件:

if (cancel == true)
{
    label6.Text = "Process Cancelled";
}
else
{
    label6.Text = "Process Completed";
}

这就是 BackgroundWebCrawling 类现在的样子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
using System.Net;
using System.Windows.Forms;
using System.ComponentModel;
using System.Threading;

namespace GatherLinks
{
    class BackgroundWebCrawling
    {
        public string f;
        int counter = 0;
        List<string> WebSitesToCrawl;
        int MaxSimultaneousThreads;
        public BackgroundWorker mainBackGroundWorker;
        BackgroundWorker secondryBackGroundWorker;
        WebcrawlerConfiguration webcrawlerCFG;
        List<WebCrawler> webcrawlers;
        int maxlevels;
        public event EventHandler<BackgroundWebCrawlingProgressEventHandler> ProgressEvent;
        ManualResetEvent _busy = new ManualResetEvent(true);

        public BackgroundWebCrawling()
        {
            webcrawlers = new List<WebCrawler>();
            mainBackGroundWorker = new BackgroundWorker();
            mainBackGroundWorker.WorkerSupportsCancellation = true;
            mainBackGroundWorker.DoWork += mainBackGroundWorker_DoWork;
        }

        private void mainBackGroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            for (int i = 0; i < WebSitesToCrawl.Count; i++)
            {
                _busy.WaitOne();
                if ((worker.CancellationPending == true))
                {
                    e.Cancel = true;
                    break;
                }
                while (counter >= MaxSimultaneousThreads)
                {
                    Thread.Sleep(10);
                }

                WebCrawler wc = new WebCrawler(webcrawlerCFG);
                webcrawlers.Add(wc);
                counter++;
                secondryBackGroundWorker = new BackgroundWorker();
                secondryBackGroundWorker.DoWork += secondryBackGroundWorker_DoWork;
                object[] args = new object[] { wc, WebSitesToCrawl[i] };
                secondryBackGroundWorker.RunWorkerAsync(args);
            }
            while (counter > 0)
            {
                Thread.Sleep(10);
            }
        }

        private void secondryBackGroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            object[] args = (object[])e.Argument;
            WebCrawler wc = (WebCrawler)args[0];
            string mainUrl = (string)args[1];
            wc.ProgressEvent += new EventHandler<WebCrawler.WebCrawlerProgressEventHandler>(x_ProgressEvent);
            wc.webCrawler(mainUrl, maxlevels);

            counter--;
        }

        public void Start(List<string> sitestocrawl, int threadsNumber, int maxlevels, WebcrawlerConfiguration wccfg)
        {
            this.maxlevels = maxlevels;
            webcrawlerCFG = wccfg;
            WebSitesToCrawl = sitestocrawl;
            MaxSimultaneousThreads = threadsNumber;
            mainBackGroundWorker.RunWorkerAsync();
        }

        private void x_ProgressEvent(object sender, WebCrawler.WebCrawlerProgressEventHandler e)
        {
            // OK .. so now you get the data here in e
            // and here you should call the event to form1
            Object[] temp_arr = new Object[8];
            temp_arr[0] = e.csFiles;
            temp_arr[1] = e.mainUrl;
            temp_arr[2] = e.levels;
            temp_arr[3] = e.currentCrawlingSite;
            temp_arr[4] = e.sitesToCrawl;
            temp_arr[5] = e.done;
            temp_arr[6] = e.failedUrls;
            temp_arr[7] = e.failed;
            OnProgressEvent(temp_arr); /// Send the data + additional data from this class to Form1..
                                       ///
            /*
             * temp_arr[0] = csFiles;
                temp_arr[1] = mainUrl;
                temp_arr[2] = levels;
                temp_arr[3] = currentCrawlingSite;
                temp_arr[4] = sitesToCrawl;*/
        }

        private void GetLists(List<string> allWebSites)
        {

        }

        public class BackgroundWebCrawlingProgressEventHandler : EventArgs
        {
            public List<string> csFiles { get; set; }
            public string mainUrl { get; set; }
            public int levels { get; set; }
            public List<string> currentCrawlingSite { get; set; }
            public List<string> sitesToCrawl { get; set; }
            public bool done { get; set; }
            public int failedUrls { get; set; }
            public bool failed { get; set; }
        }

        protected void OnProgressEvent(Object[] some_params) // Probably you need to some vars here to...
        {
            // some_params to put in evenetArgs..
            if (ProgressEvent != null)
                ProgressEvent(this,
                    new BackgroundWebCrawlingProgressEventHandler()
                    {
                        csFiles = (List<string>)some_params[0],
                        mainUrl = (string)some_params[1],
                        levels = (int)some_params[2],
                        currentCrawlingSite = (List<string>)some_params[3],
                        sitesToCrawl = (List<string>)some_params[4],
                        done = (bool)some_params[5],
                        failedUrls = (int)some_params[6],
                        failed = (bool)some_params[7]
                    });
        }

        public void PauseWorker()
        {
            if (mainBackGroundWorker.IsBusy)
            {
                _busy.Reset();
            }
        }

        public void ContinueWorker()
        {
            _busy.Set();
        }

        public void CancelWorker()
        {
            ContinueWorker();
            mainBackGroundWorker.CancelAsync();
        }

    }
}

所以我添加了暂停继续取消的方法。在dowork事件中,我改变了所有的东西并添加了东西。

但是当我点击按钮时没有效果。不暂停,不继续,不取消。没有什么。

4

1 回答 1

3

您从不检查方法中的_busy状态mainBackGroundWorker_DoWork

for (int i = 0; i < WebSitesToCrawl.Count; i++)
{
    _busy.WaitOne();
    //...
}

你也应该ManualResetEvent _busy和BackgroundWorker一起上课

ManualResetEvent _busy = new ManualResetEvent(true);
public BackgroundWorker mainBackGroundWorker;

public void PauseWorker()
{
   if(mainBackGroundWorker.IsBusy)
   {
        _busy.Reset(); 
   }
}

public void ContinueWorker()
{
    _busy.Set();
}

并在Form1

private void button4_Click(object sender, EventArgs e)
{
    bgwc.PauseWorker();
    //...
}

private void button5_Click(object sender, EventArgs e)
{
    bgwc.ContinueWorker();
    //...
}

要取消 BackgroundWorker,您可以使用CancellationPending属性和CancelAsync方法。注意:您应该首先取消暂停工作人员。

public void CancelWorker()
{
   ContinueWorker();
   mainBackGroundWorker.CancelAsync();
}

private void mainBackGroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = sender as BackgroundWorker;
    for (int i = 0; i < WebSitesToCrawl.Count; i++)
    {
        _busy.WaitOne();
         if ((worker.CancellationPending == true))
         {
             e.Cancel = true;
             break;
         }
         //...
    }  
}

如果这对您没有帮助,那么您的mainBackGroundWorker代码和secondryBackGroundWorker.

  1. 此代码仅暂停 mainBackGroundWorker,但不暂停 secondryBackGroundWorkers。取消也是一样。如果主要工人被取消?它将等待所有二级工人完成他们的工作。另外,如果您暂停主要工作人员?你仍然可以从二级工人那里得到新的结果。

  2. 你不处理错误。如果您在第二个工作人员中有异常,那么您不会收到任何通知,并且您的主要工作人员将永远不会停止,因为counter永远不会0

  3. 可能还有另一个问题,女巫会导致这种行为。

于 2013-09-10T22:38:02.210 回答