How can i pause/resume a loop in a backgroundworker?
<p>In the top of form1</p>

<pre><code>private System.Threading.ManualResetEvent _busy = new System.Threading.ManualResetEvent(false);
private System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
</code></pre>

<p>A button click event that start the backgroundworker and the timer</p>

<pre><code>private void startButton_Click(object sender, EventArgs e)
        {
            startButton.Enabled = false;
            stopButton.Enabled = true;
            timer1.Start();
            if (!backgroundWorker1.IsBusy)
                backgroundWorker1.RunWorkerAsync();
        }
</code></pre>

<p>Dowork event</p>

<pre><code>private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            _stopwatch.Restart();
            DirSearch(@"d:\c-sharp", "*.cs", "Form1", worker, e);
            _stopwatch.Stop();
        }
</code></pre>

<p>DirSearch method</p>

<pre><code>private void DirSearch(string root, string filesExtension,string textToSearch, BackgroundWorker worker, DoWorkEventArgs e)
        {
            int numberoffiles = 0;
            int numberofdirs = 0;
            string[] filePaths = Directory.GetFiles(root, filesExtension, SearchOption.AllDirectories);
            for (int i = 0; i &lt; filePaths.Length; i++)
            {
                _busy.WaitOne();
                if (worker.CancellationPending == true)
                {
                    e.Cancel = true;
                    return;
                }

                List&lt;MyProgress&gt; prog = new List&lt;MyProgress&gt;();
                int var = File.ReadAllText(filePaths[i]).Contains(textToSearch) ? 1 : 0;
                if (var == 1)
                {
                    string filename = filePaths[i];
                    numberoffiles ++;
                    prog.Add(new MyProgress { Report1 = filename, Report2 = numberoffiles.ToString() });
                    backgroundWorker1.ReportProgress(0, prog);
                    Thread.Sleep(100);
                }
                numberofdirs ++;
                label1.Invoke((MethodInvoker)delegate
                            {
                                label1.Text = numberofdirs.ToString();
                                label1.Visible = true;
                            });
            }
        }
</code></pre>

<p>Button click event for pause/resume</p>

<pre><code>private void pauseresumeButton_Click(object sender, EventArgs e)
        {
            if (pauseresumeButton.Text == "Resume")
            {
                pauseresumeButton.Text = "Pause";
                _busy.Set();
            }
            else
            {
                pauseresumeButton.Text = "Resume";
                _busy.Reset();
            }
        }
</code></pre>

<p>The pauseresumeButton text in designer is set to "Pause"</p>

<p>And the problem is that the <code>_busy.WaitOne();</code> inside the DirSearch make it that when i click the start button only the timer is running. Then i need to click the pauseresumeButton to start it like resume it.</p>

<p>What i want to do is when i click the start button everything will start the timer and the DirSearch. And then if i click on the pauseresumeButton to pause the backgroundworker the timer and the DirSearch operation and another click on pauseresumeButton to resume from where it all stopped.</p>