How to start and stop a continuous background worker using a button

Let's say I have a desktop like this:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { while(true) { //Kill zombies } } 

How can I get this background worker to start and stop using a button in WinForm?

+6
c # backgroundworker winforms
source share
5 answers

Here's how to do it (link to answer below)

+5
source share

Perhaps you can use manualresetevent like this, I did not debug this, but it is worth taking a picture. If this works, you wonโ€™t have the thread spinning the wheels while she waits

 ManualResetEvent run = new ManualResetEvent(true); private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { while(run.WaitOne()) { //Kill zombies } } private void War() { run.Set(); } private void Peace() { run.Reset(); } 
+17
source share

Use the CancelAsync method.

 backgroundworker1.CancelAsync(); 

In your loop inside the workflow.

 if (backgroundWorker.CancellationPending) return; 

This does not happen right away.

+7
source share

Try, do you really mean a stop or do you mean a pause? If you mean a stop, then this is a piece of cake. Create a button click event handler for the button on which you want to be responsible for launching the background, and a button click event handler for the person responsible for stopping it. On the start button, make a call to the desktop method that fires the do_work event. Something like that:

 private void startButton_Click(System.Object sender, System.EventArgs e) { // Start the asynchronous operation. backgroundWorker1.RunWorkerAsync(); } 

On your stop button, call a method that sets the background working CancellationPending to true, for example:

 private void cancelAsyncButton_Click(System.Object sender, System.EventArgs e) { // Cancel the asynchronous operation. this.backgroundWorker1.CancelAsync(); } 

Now remember to check the CancelationPending flag inside your desktop. Something like that:

  private void KillZombies(BackgroundWorker worker, DoWorkEventArgs e) { while (true) { if (worker.CancellationPending) { e.Cancel = true; } } } 

And your doWork method:

 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; KillZombies(worker, e); } 

Hope this helps you in the right direction. Some additional readings:

http://msdn.microsoft.com/en-us/library/b2zk6580(v=VS.90).aspx

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

http://msdn.microsoft.com/en-us/library/waw3xexc.aspx

+4
source share

I have not tested this, I have code somewhere that I will have to see what I have done, but something like this is an adaptation of Fredrick's answer:

 private bool _performKilling; private object _lockObject = new object(); private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { while(true) { if (_performKilling) { //Kill zombies } else { //We pause until we are woken up so as not to consume cycles Monitor.Wait(_lockObject); } } } private void StartKilling() { _performKilling = true; Monitor.Pulse(_lockObject); } private void StopAllThatKilling() { _performKilling = false; ] 

A more complete example of this template is here:

https://github.com/AaronLS/CellularAutomataAsNeuralNetwork/blob/fe9e6b950e5e28d2c99350cb8ff3157720555e14/CellLifeGame1/Modeling.cs

0
source share

All Articles