How to Wait for a Backgroundworker to Cancel

How to wait for a BackgroundWorker to cancel?

If I understand your requirement right, you could do something like this (code not tested, but shows the general idea):

private BackgroundWorker worker = new BackgroundWorker();
private AutoResetEvent _resetEvent = new AutoResetEvent(false);

public Form1()
{
InitializeComponent();

worker.DoWork += worker_DoWork;
}

public void Cancel()
{
worker.CancelAsync();
_resetEvent.WaitOne(); // will block until _resetEvent.Set() call made
}

void worker_DoWork(object sender, DoWorkEventArgs e)
{
while(!e.Cancel)
{
// do something
}

_resetEvent.Set(); // signal that worker is done
}

How to wait for BackgroundWorker to finish and then exit console application

See the How to wait for a BackgroundWorker to cancel? post for how to communicate between your BackgroundWorker and your main thread.

Basically, you have to use a event that you set at the end of DoWork to signal that DoWork has completed. You then WaitOne() on that event in your main thread.

How to cancel a thread waiting for a background worker?


Using another background worker

As it seems, you're more comfortable working with a background worker. You can use another background worker instead of a thread here.

So, instead of creating threads:

Thread t1 = new Thread(waitingMethod);

You can create a background worker and start it here:

waitingWorker.RunWorkerAsync();

Then you can handle worker's events and use its CancellationPending flag to cancel a thread:

private void waitingWorker_DoWork(object sender, DoWorkEventArgs e)
{
while (arrFibo[eingabe] == 0 && !waitingWorker.CancellationPending)
{
Thread.Sleep(500);
Console.WriteLine("Treadsleeping " + arrFibo[eingabe]);
}
}
private void waitingWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
labelErgebnis.Invoke(new Action(() => labelErgebnis.Text = arrFibo[eingabe].ToString()));
...
}

Using a thread

If you still want to use a thread, then you'll need to implement a cancellation flag yourself. You can apply one of the solutions from an earlier question: How to Cancel a Thread?

As for now, _shouldStop is never used in your code. You can use it as a cancellation flag. Using the code from @Fredrik Mörk's answer

bool _shouldStop = false;
private static object _shouldStopLock = new object();

//in buttonStartStop_Click2
lock (_shouldStopLock)
{
_shouldStop = false;
}

//in waiting method
bool localShouldStop = false;
while (arrFibo[eingabe] == 0 && !localShouldStop)
{
Thread.Sleep(500);
Console.WriteLine("Treadsleeping " + arrFibo[eingabe]);
lock (_shouldStopLock)
{
localShouldStop = _shouldStop;
}
}

C# BackgroundWorker Wait Until Ends

Listen for the completed event, and then restart the worker there -

private bool restartOnCancel = false;

private void button3_Click_1(object sender, EventArgs e)
{
restartOnCancel = true;
backgroundWorker1.CancelAsync();
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if ((e.Cancelled == true) && restartOnCancel)
{
restartOnCancel = false;
backgroundWorker1.RunWorkerAsync();
}
}

Wait for BackgroundWorker finish, if running, at FormClosing time

No need to make it so complex, just have a class level variable

bool quitRequestedWhileWorkerBusy=false;

If the user tried to close the form, in the form closing event check if the worker isbusy, cancel the event and set quitRequestedWhileWorkerBusy=true

In your worker completed event, if(quitRequestedWhileWorkerBusy) this.Close();

How to kill a BackgroundWorker process when it is waiting for an external event

EDIT: To force the SCardGetStatusChange function to cancel, use SCardCancel: https://msdn.microsoft.com/en-us/library/windows/desktop/aa379470(v=vs.85).aspx



Related Topics



Leave a reply



Submit