Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pause and resume a BackgroundWorker?

Tags:

c#

This is how I did it in my code: In the backgroundWorker DoWork event I did:

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; }));
                webCrawler(mainUrl, levelsToCrawl, e);


        }

Then in the pause button click event I did:

private void button4_Click(object sender, EventArgs e)
        {
            _busy.Reset();
        }

In the resume button click event I did:

private void button5_Click(object sender, EventArgs e)
        {
            _busy.Set();
        }

But it's not working when I click to start the process:

private void button1_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
            button1.Enabled = false;
            this.Text = "Processing...";
            label6.Text = "Processing...";
            label6.Visible = true;
            button2.Enabled = false;
            checkBox1.Enabled = false;
            checkBox2.Enabled = false;
            numericUpDown1.Enabled = false;
            button3.Enabled = true;
        }

Nothing happen only when I click the resume button the process start then when I click the pause button nothing happen.

I want that when I click the start process button it will start the backgroundWorker regular then when clicking the pause button it will pause and the resume button it will resume.

What did I do wrong ? Can someone fix my code ?

like image 294
Daniel Lip Avatar asked Oct 08 '12 10:10

Daniel Lip


People also ask

How do I pause my background worker?

if you want to pause and resume a BGW, split your task into smaller packets (or put the needed values (like a counter variable/state) into e. Result), stop the BGW at a safe point and in the RunWorkerCompleted event, dispose the worker, instanciate it again (or a second BGW) and fire it up when needed.

What is the difference between BackgroundWorker and thread?

A BackgroundWorker is a ready to use class in WinForms allowing you to execute tasks on background threads which avoids freezing the UI and in addition to this allows you to easily marshal the execution of the success callback on the main thread which gives you the possibility to update the user interface with the ...

Is BackgroundWorker threaded?

BackgroundWorker, is a component in . NET Framework, that allows executing code as a separate thread and then report progress and completion back to the UI.

What is BackgroundWorker?

The BackgroundWorker class allows you to run an operation on a separate, dedicated thread. Time-consuming operations like downloads and database transactions can cause your user interface (UI) to seem as though it has stopped responding while they are running.


1 Answers

In your BackgroundWorker thread code, you need to find places that are safe to "pause" execution. The ManualResetEvent is the right way to code. This other post might help: Is there a way to indefinitely pause a thread?

Basically, in a few choice points in your worker thread code where you want to allow it to pause, try inserting:

_busy.WaitOne(Timeout.Infinite);

And when you want to pause (from your main thread) you use:

_busy.Reset();

And to resume:

_busy.Set();
like image 53
dbattaglia Avatar answered Sep 21 '22 11:09

dbattaglia