How can i stop/resume a backgroundworker/recursive loop?
<p>In the top of the form1 i added:</p>

<pre><code>System.Threading.ManualResetEvent _busy = new System.Threading.ManualResetEvent(true);
</code></pre>

<p>And also in the top of the form1 added two flags:</p>

<pre><code>bool pause;
bool resume;
</code></pre>

<p>Both of the flags are set to false in the constructor.</p>

<p>Then i have a function that is recursive loop:</p>

<pre><code>private List&lt;string&gt; test(string url, int levels,DoWorkEventArgs eve)
        {
            if (pause == true)
            {
                _busy.Reset();
            }
            if (resume == true)
            {
                _busy.Set();
            }
            this.Invoke(new MethodInvoker(delegate { label3.Text = label3.Text = (Int32.Parse(label12.Text) + Int32.Parse(label10.Text)).ToString(); }));

            HtmlWeb hw = new HtmlWeb();
            List&lt;string&gt; webSites;
            List&lt;string&gt; csFiles = new List&lt;string&gt;();

            csFiles.Add("temp string to know that something is happening in level = " + levels.ToString());
            csFiles.Add("current site name in this level is : " + url);

            try
            {
                this.Invoke(new MethodInvoker(delegate { ColorText.Texts(richTextBox1, "Loading The Url:   " , Color.Red); }));
                this.Invoke(new MethodInvoker(delegate { ColorText.Texts(richTextBox1, url + "...",Color.Blue); }));
                HtmlAgilityPack.HtmlDocument doc = TimeOut.getHtmlDocumentWebClient(url, false, "", 0, "", "");

                this.Invoke(new MethodInvoker(delegate { ColorText.Texts(richTextBox1, " Done " + Environment.NewLine, Color.Red); }));


                    currentCrawlingSite.Add(url);
                    webSites = getLinks(doc);
                    removeDupes(webSites);
                    removeDuplicates(webSites, currentCrawlingSite);
                    removeDuplicates(webSites, sitesToCrawl);
                    if (removeExt == true)
                    {
                       for (int i = 0; i &lt; webSites.Count; i++)
                       {
                           webSites.Remove(removeExternals(webSites));
                       }
                    }
                    if (downLoadImages == true)
                    {
                        webContent.retrieveImages(url);                     }

                    if (levels &gt; 0)
                        sitesToCrawl.AddRange(webSites
                    this.Invoke(new MethodInvoker(delegate { label7.Text = sitesToCrawl.Count.ToString(); }));
                    this.Invoke(new MethodInvoker(delegate { label12.Text = currentCrawlingSite.Count.ToString(); }));


                    if (levels == 0)
                    {
                        return csFiles;
                    }
                    else
                    {


                        for (int i = 0; i &lt; webSites.Count(); i++)//&amp;&amp; i &lt; 20; i++)                         {



                            string t = webSites[i];
                            if ((t.StartsWith("http://") == true) || (t.StartsWith("https://") == true)) // replace this with future FilterJunkLinks function
                            {
                                csFiles.AddRange(test(t, levels - 1, eve));                                
                            }

                        }
                        return csFiles;
                    }



            }
            catch
            {
                failedUrls++;
                this.Invoke(new MethodInvoker(delegate { label10.Text = failedUrls.ToString(); }));
                this.Invoke(new MethodInvoker(delegate { ColorText.Texts(richTextBox1, " Failed " + Environment.NewLine, Color.Green); }));
                return csFiles;
            }

        }
</code></pre>

<p>In the backgroundworker DoWork event i added this:</p>

<pre><code>private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            _busy.WaitOne();
                this.Invoke(new MethodInvoker(delegate { label2.Text = "Website To Crawl: "; }));
                this.Invoke(new MethodInvoker(delegate { label4.Text = mainUrl; }));
                test(mainUrl, levelsToCrawl, e);
        }
</code></pre>

<p>Then i have two buttons click events one for pause one for resume:</p>

<pre><code>private void button4_Click(object sender, EventArgs e)
        {
            pause = true;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            resume = true;
        }
</code></pre>

<p>But when i click on the button pause nothing happens either on the resume button. The process is keep going on. I tried also without flags just in each button to make _busy.Reset(); and _busy.Set(); but nothing.</p>

<p>What i want is to pause somehow the recursive loop and to resume it. Im not sure if its connected to the backgroundworker or not but the idea is to pause and resume the process.</p>

<p>What should i do ?</p>

<p>Thanks.</p>
