❔ How to disable background worker?
Hi,
I need to do something in background when state is true and stop when state is false. I changing state by button. If I press button, state is true unit I press button again. I set WorkerSupportsCancellation on true. When I press button again, I have all the time TEST in console. Any idea?
I need to do something in background when state is true and stop when state is false. I changing state by button. If I press button, state is true unit I press button again. I set WorkerSupportsCancellation on true. When I press button again, I have all the time TEST in console. Any idea?
private void Start_Click(object sender, EventArgs e)
{
if (started)
{
StartButton.Text = "Start";
started = false;
backgroundWorker1.CancelAsync();
InitCheckLocalTimer().Stop();
InitCheckLocalTimer().Dispose();
}
else
{
StartButton.Text = "Stop";
started = true;
InitCheckLocalTimer().Start();
}
}
private void CheckLocalTimer(object sender, EventArgs e)
{
if (!backgroundWorker1.IsBusy)
{
backgroundWorker1.RunWorkerAsync();
}
}
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
if (!backgroundWorker1.CancellationPending && !e.Cancel)
{
Console.WriteLine("TEST")
}
else
{
e.Cancel = true;
}
}private void Start_Click(object sender, EventArgs e)
{
if (started)
{
StartButton.Text = "Start";
started = false;
backgroundWorker1.CancelAsync();
InitCheckLocalTimer().Stop();
InitCheckLocalTimer().Dispose();
}
else
{
StartButton.Text = "Stop";
started = true;
InitCheckLocalTimer().Start();
}
}
private void CheckLocalTimer(object sender, EventArgs e)
{
if (!backgroundWorker1.IsBusy)
{
backgroundWorker1.RunWorkerAsync();
}
}
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
if (!backgroundWorker1.CancellationPending && !e.Cancel)
{
Console.WriteLine("TEST")
}
else
{
e.Cancel = true;
}
}