S
Steve
After running into a design wall with my traditional thread approach, I
realized that a BackgroundWorker object would fit my needs pretty good.
Never really used them before.
In a nutshell, my problem is cancelling the worker. I've read the MSDN
docs, checked google and haven't found a solution.
The problem is, once I make a call to cancellAsync() (I check for
CancellationPending in my DoWork handler) and WorkerComplete fires, the
value of Cancelled in the event args is always false.
Here is my code:
<code>
public void UpdateView()
{
if(MSP430Programmer.TargetConnected())
{
View.CloseViewWithResult(DialogResult.OK);
}
View.SetMessageText(ProgramTargetResources.TargetDisconnectedMessage);
_pollingWorker = new BackgroundWorker();
_pollingWorker.WorkerSupportsCancellation = true;
_pollingWorker.DoWork += OnPollingWorkerDoWork;
_pollingWorker.RunWorkerCompleted += OnPollingWorkerComplete;
_pollingWorker.RunWorkerAsync();
}
private void OnPollingWorkerDoWork(object sender, DoWorkEventArgs args)
{
while(_pollingWorker.CancellationPending == false &&
MSP430Programmer.TargetConnected() == false)
{
Thread.Sleep(500);
}
}
***** When I cancel (and it works cause this fires before
MSP430Programmer.TargetConnected() evaluates true
***** the Cancelled property of args is false.
private void OnPollingWorkerComplete(object sender,
RunWorkerCompletedEventArgs args)
{
View.CloseViewWithResult(DialogResult.OK);
}
public void CancelAndClose()
{
_pollingWorker.CancelAsync();
}
</code>
Anyone see the problem? What am I missing? Is it a bug? I'm no threading
expert, in face I barely grasp the concepts, but from what I think I know
this isn't a race condition as MSP430Programmer.TargetConnected() is always
false.
Thanks for reading,
Steve
realized that a BackgroundWorker object would fit my needs pretty good.
Never really used them before.
In a nutshell, my problem is cancelling the worker. I've read the MSDN
docs, checked google and haven't found a solution.
The problem is, once I make a call to cancellAsync() (I check for
CancellationPending in my DoWork handler) and WorkerComplete fires, the
value of Cancelled in the event args is always false.
Here is my code:
<code>
public void UpdateView()
{
if(MSP430Programmer.TargetConnected())
{
View.CloseViewWithResult(DialogResult.OK);
}
View.SetMessageText(ProgramTargetResources.TargetDisconnectedMessage);
_pollingWorker = new BackgroundWorker();
_pollingWorker.WorkerSupportsCancellation = true;
_pollingWorker.DoWork += OnPollingWorkerDoWork;
_pollingWorker.RunWorkerCompleted += OnPollingWorkerComplete;
_pollingWorker.RunWorkerAsync();
}
private void OnPollingWorkerDoWork(object sender, DoWorkEventArgs args)
{
while(_pollingWorker.CancellationPending == false &&
MSP430Programmer.TargetConnected() == false)
{
Thread.Sleep(500);
}
}
***** When I cancel (and it works cause this fires before
MSP430Programmer.TargetConnected() evaluates true
***** the Cancelled property of args is false.
private void OnPollingWorkerComplete(object sender,
RunWorkerCompletedEventArgs args)
{
View.CloseViewWithResult(DialogResult.OK);
}
public void CancelAndClose()
{
_pollingWorker.CancelAsync();
}
</code>
Anyone see the problem? What am I missing? Is it a bug? I'm no threading
expert, in face I barely grasp the concepts, but from what I think I know
this isn't a race condition as MSP430Programmer.TargetConnected() is always
false.
Thanks for reading,
Steve