6

I have a loop in Background worker in a Winform Application.

I Just used this Code but it won't resume after the Pause.

In the main Class I use this

System.Threading.ManualResetEvent _busy = new System.Threading.ManualResetEvent(false);

Then in My Start Click I wrote this:

      if (!backgroundWorker1.IsBusy)
            {
                MessageBox.Show("Not Busy"); //Just For Debugg
                _busy.Set();
                Start_Back.Text = "Pause";
                backgroundWorker1.RunWorkerAsync(tempCicle);   
            }
            else
            {
                _busy.Reset();
                Start_Back.Text = "Resume";
            }

            btnStop.Enabled = true;

Then in backgroundworker doWork I wrote this:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
     m_addTab addTabsInvoke = addTabUrl2;
      Invoke(addTabsInvoke, "http://www.google.com");
        foreach (something)
                {
                    _busy.WaitOne();

                    if (backgroundWorker1.CancellationPending)
                    {
                        return;
                    }
                    if (tabs.InvokeRequired)
                        {
    ......
    ......

I can't understand why pause works while resume doesn't work. Did I wrong something?

Community
  • 1
  • 1
Jasper
  • 311
  • 2
  • 5
  • 16
  • 1
    Reset() will cause the DoWork method to hang, blocking on the WaitOne() call. The opposite of a resume, I'd say. Hard to see what you are trying to do. – Hans Passant Dec 02 '11 at 16:18
  • I want resume and pause a loop in background worker. I just followed the code that i linked.http://stackoverflow.com/questions/3924275/pause-and-resume-a-for-loop-in-c-sharp – Jasper Dec 02 '11 at 16:20
  • 1
    Maybe i understood the problem. After that i click on Pause , i will never do Set() again cause isBusy will always be true so it will never do If code...i think – Jasper Dec 02 '11 at 16:33
  • Maybe it would be better if you told us what you're trying to accomplish and we can recommend a solution for you. If you are trying to update progress from a background worker, you should look at [ProgressChanged](http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.progresschanged.aspx) and [ReportProgress](http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.reportprogress.aspx). – mattypiper Dec 02 '11 at 19:57

1 Answers1

10

My best guess for what you want:

void ResumeWorker() {
     // Start the worker if it isn't running
     if (!backgroundWorker1.IsBusy) backgroundWorker1.RunWorkerAsync(tempCicle);  
     // Unblock the worker 
     _busy.Set();
}

void PauseWorker() {
    // Block the worker
    _busy.Reset();
}

void CancelWorker() {
    if (backgroundWorker1.IsBusy) {
        // Set CancellationPending property to true
        backgroundWorker1.CancelAsync();
        // Unblock worker so it can see that
        _busy.Set();
    }
}
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536