R
Rich P
I run an operation using a BackgroundWorker control. I have a button
that I toggle to stop an operation or restart it. Here is the code to
do that:
private void btn_StartStop(...)
{
if (BackgroundWorker1.IsBusy)
{ //stop the operation
BackgroundWorker1.CancelAsync();
btn_StartStop.Text = "Restart Operation";
}
else
{
//restart operation
... do some stuff
BackgroundWorker1.RunWorkerAsync();
btn_StartStop.Text = "Stop Operation";
}
}
The problem I am having is that when I first stop the operation and then
try to restart it -- the code will step right through
//to turn BackroundWorker1 back on
BackgroundWorker1.RunWorkerAsync();
The code recognizes that
BackgroundWorker1.IsBusy.Equals(false)
and it will change the button text to "Stop Operation" -- which suggests
the operation is running --
BackgroundWorker1.IsBusy.Equals(true)
//but this is actually still false -- it's not busy yet
and the code never invokes BackgroundWorker1_DoWork() on the first
click. If I re-click the StartStop button -- it sees that
BackgroundWorker1.IsBusy.Equals(false)
is still the case and this time it WILL invoke
BackgroundWorker1_DoWork()
Subsequent start/stop's work OK. When you click start - it restarts the
operation on the first clikc.
Does anyone have any idea how I can make the first attempt to restart
the operation fire BackgroundWorker1_DoWork() on the first click?
Rich
that I toggle to stop an operation or restart it. Here is the code to
do that:
private void btn_StartStop(...)
{
if (BackgroundWorker1.IsBusy)
{ //stop the operation
BackgroundWorker1.CancelAsync();
btn_StartStop.Text = "Restart Operation";
}
else
{
//restart operation
... do some stuff
BackgroundWorker1.RunWorkerAsync();
btn_StartStop.Text = "Stop Operation";
}
}
The problem I am having is that when I first stop the operation and then
try to restart it -- the code will step right through
//to turn BackroundWorker1 back on
BackgroundWorker1.RunWorkerAsync();
The code recognizes that
BackgroundWorker1.IsBusy.Equals(false)
and it will change the button text to "Stop Operation" -- which suggests
the operation is running --
BackgroundWorker1.IsBusy.Equals(true)
//but this is actually still false -- it's not busy yet
and the code never invokes BackgroundWorker1_DoWork() on the first
click. If I re-click the StartStop button -- it sees that
BackgroundWorker1.IsBusy.Equals(false)
is still the case and this time it WILL invoke
BackgroundWorker1_DoWork()
Subsequent start/stop's work OK. When you click start - it restarts the
operation on the first clikc.
Does anyone have any idea how I can make the first attempt to restart
the operation fire BackgroundWorker1_DoWork() on the first click?
Rich