473,394 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

RunWorkerCompletedEventArgs.Cancelled always false, even when I know it caught the cancel request

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.TargetD isconnectedMessage);

_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
Jun 8 '06 #1
2 5365
When CancellationPending is true in your OnPollingWorkerDoWork method
you need to set args.cancel = true
Steve wrote:
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.TargetD isconnectedMessage);

_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


Jun 9 '06 #2
Thanks for clearing that up, I apprecaite it.
I guess I was thinking it would set that for me, but I suppose just cause I
request it to cancel doesn't mean it will, so you need to explicitly set it?
Hmm...

Thanks again,
Steve
<sd********@gmail.com> wrote in message
news:11**********************@h76g2000cwa.googlegr oups.com...
When CancellationPending is true in your OnPollingWorkerDoWork method
you need to set args.cancel = true
Steve wrote:
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.TargetD isconnectedMessage);

_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

Jun 9 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: jeffrod444 | last post by:
Hi, I've been having problems getting my forms to work correctly. If seen other threads addressing this issue, however, these are not working for me. The short of my problem is this. I am trying to...
6
by: Shyguy | last post by:
I have a form where one of the entries is "Contact". On BeforeUpdate I check the table for the existance of that Contact. A mesage box informs the user that the name is already in the table and...
1
by: JM | last post by:
i have an aspx page that imports 15k records from ms access into sql server. the page is running pretty slow, so i have to reevaulate the code. I tested the page, while is was processing, i hit the...
2
by: Edward | last post by:
For reporting purposes I use Crystal Reports. The user can select a printer from a dropdownlist. I set the printer with: PrintOptions.PrinterName = @"\\SERVERNAME\" +...
3
by: Chandra | last post by:
What happens to a cancelled http request from the server perspective? Here's the scenario. I request a page from the web server and page requires lot of processing. Before the response comes...
14
by: Ron L | last post by:
All I am working with a DataGrid and a form consisting of a number of text, checkbox, combobox controls, all bound to the same datatable. When I click on my "New" button, I create a new row,...
1
by: Mesan | last post by:
Has anyone else noticed that even though you handle an exception that happens within the background worker that the UnhandledException event still fires? For example, here's my ThreadException...
3
by: perrog | last post by:
Hi! What is the expected behaviour when you send an XmlHttpRequest just before the page is about to unload? I'm sending a XmlHttpRequest on an onClick event, and I can inspect that the request...
4
by: Phil Stanton | last post by:
I am opening a report (in Preview) from a menu system I use the following code if there is no data in a report Private Sub Report_NoData(Cancel As Integer) MsgBox "There are no errors in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.