473,511 Members | 15,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cancelling My Thread (from within)

I've got a thread that pulls data from the SQL Server.

After running a query, if the DataTable has records, I go on to process them.

If the DataTable does not have records, I want to exit the thread.

How do I exit the thread? I have tried:
/*****************************/
if (table.Rows.Count == 0) {
e.Cancel = true; // DoWorkEvenArgs e
e.Result = "No Records were found";
return;
}
// continue with record processing part of thread
/*****************************/

However, I get an exception thrown that my app can not handle, and it throws
it all the way up to Program.cs:

try {
Application.Run(new MdiForm());
} catch (Exception e) {
MessageBox.Show(e.Message);
}

e.Message = "Exception has been thrown by the target of an invocation."

That doesn't help me much, but maybe someone here can use it.
Aug 15 '08 #1
7 1646
On Aug 15, 11:05*pm, jp2msft <jp2m...@discussions.microsoft.com>
wrote:
I've got a thread that pulls data from the SQL Server.

After running a query, if the DataTable has records, I go on to process them.

If the DataTable does not have records, I want to exit the thread.

How do I exit the thread? I have tried:
/*****************************/
if (table.Rows.Count == 0) {
* e.Cancel = true; // DoWorkEvenArgs e
* e.Result = "No Records were found";
* return;}

// continue with record processing part of thread
/*****************************/

However, I get an exception thrown that my app can not handle, and it throws
it all the way up to Program.cs:

try {
* Application.Run(new MdiForm());} catch (Exception e) {

* MessageBox.Show(e.Message);

}

e.Message = "Exception has been thrown by the target of an invocation."

That doesn't help me much, but maybe someone here can use it.
You should have a look at the InnerException of that exception - it
will tell you the actual cause of the problem.

Other than that, what was the actual question?
Aug 15 '08 #2
On Fri, 15 Aug 2008 12:05:03 -0700, jp2msft
<jp*****@discussions.microsoft.comwrote:
[...]
How do I exit the thread? I have tried:
/*****************************/
if (table.Rows.Count == 0) {
e.Cancel = true; // DoWorkEvenArgs e
e.Result = "No Records were found";
return;
}
[...]
e.Message = "Exception has been thrown by the target of an invocation."

That doesn't help me much, but maybe someone here can use it.
What is the InnerException of that exception? That should lead you to
more information as to why the exception actually happened. Since you
didn't post all of the code, it's practically impossible for us to know
exactly what went wrong. We can make some guesses, but there's usually
just as much chance of guessing wrong as right, so I generally don't like
to bother.

That said, I don't think you really want to set the Cancel property when
you exit your thread. That's usually set when the background task was
explicitly cancelled. That didn't happen in this case; you just didn't
find anything to process. If for no other reason than that if the DoWork
handler cancels the event, you can't access the Result property without an
exception, it seems counter-productive to me to be setting the Cancel
property.

Pete
Aug 15 '08 #3
Solved it myself, actually.

Every time my Thread_RunWorkerCompleted attempted to read the reason
specified by e.Reason below, it threw an exception.

My Solution: I stopped reading it!

"jp2msft" wrote:
I've got a thread that pulls data from the SQL Server.

After running a query, if the DataTable has records, I go on to process them.

If the DataTable does not have records, I want to exit the thread.

How do I exit the thread? I have tried:
/*****************************/
if (table.Rows.Count == 0) {
e.Cancel = true; // DoWorkEvenArgs e
e.Result = "No Records were found";
return;
}
// continue with record processing part of thread
/*****************************/

However, I get an exception thrown that my app can not handle, and it throws
it all the way up to Program.cs:

try {
Application.Run(new MdiForm());
} catch (Exception e) {
MessageBox.Show(e.Message);
}

e.Message = "Exception has been thrown by the target of an invocation."

That doesn't help me much, but maybe someone here can use it.
Aug 15 '08 #4
On Fri, 15 Aug 2008 14:16:09 -0700, jp2msft
<jp*****@discussions.microsoft.comwrote:
Solved it myself, actually.

Every time my Thread_RunWorkerCompleted attempted to read the reason
specified by e.Reason below, it threw an exception.

My Solution: I stopped reading it!
If you're not going to read it, why do you set it?
Aug 15 '08 #5
"Peter Duniho" wrote:
On Fri, 15 Aug 2008 14:16:09 -0700, jp2msft
If you're not going to read it, why do you set it?
Actually, since I couldn't figure out how to read it, I stopped setting it,
too!

Is there a way to set the e.Result field so that my RunWorkerCompleted event
*can* read this back in?
Aug 15 '08 #6
On Aug 15, 10:05*pm, jp2msft <jp2m...@discussions.microsoft.com>
wrote:
I've got a thread that pulls data from the SQL Server.

After running a query, if the DataTable has records, I go on to process them.

If the DataTable does not have records, I want to exit the thread.

How do I exit the thread? I have tried:
/*****************************/
if (table.Rows.Count == 0) {
* e.Cancel = true; // DoWorkEvenArgs e
* e.Result = "No Records were found";
* return;}

// continue with record processing part of thread
/*****************************/

However, I get an exception thrown that my app can not handle, and it throws
it all the way up to Program.cs:

try {
* Application.Run(new MdiForm());} catch (Exception e) {

* MessageBox.Show(e.Message);

}

e.Message = "Exception has been thrown by the target of an invocation."

That doesn't help me much, but maybe someone here can use it.
Hello

Why are you want to manage threads when you can get stuff asynchrony
and use the complete event and if e.result == 0
Do what you want
And use the .net timer to run this function
Aug 15 '08 #7
On Fri, 15 Aug 2008 14:42:02 -0700, jp2msft
<jp*****@discussions.microsoft.comwrote:
"Peter Duniho" wrote:
>On Fri, 15 Aug 2008 14:16:09 -0700, jp2msft
If you're not going to read it, why do you set it?

Actually, since I couldn't figure out how to read it, I stopped setting
it,
too!

Is there a way to set the e.Result field so that my RunWorkerCompleted
event
*can* read this back in?
Yes. As I noted in my previous reply, just don't set
DoWorkEventArgs.Cancel. I don't think that's appropriate in your
situation anyway.

Pete
Aug 16 '08 #8

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

Similar topics

1
1858
by: Paul THompson | last post by:
I have been working for some time to 1) detect tab and shift tab events 2) control the focus on the basis of these events. I have found that I can do this, but continue to have nagging problems. ...
16
1350
by: Michael Winter | last post by:
Other than throwing an exception during the execution of an object's constructor, is there any way to cancel creation of the object in such a way that it leaves a reference to it null or undefined....
1
7335
by: VM | last post by:
How can I cancel all running processes in my application when I click on the Cancel button? When the Run button's clicked, it creates an instance of a specified class and runs a method that reads...
7
2671
by: Charles Law | last post by:
My first thought was to call WorkerThread.Suspend but the help cautions against this (for good reason) because the caller has no control over where the thread actually stops, and it might have...
0
874
by: stand__sure | last post by:
Although I am aware of methods to kill an existing thread that is explicitly created in code, I am wondering if a method exists to do so with a pool thread that would be used when calling...
3
3678
by: EnglishMan69 | last post by:
Hello All, I am using VB2005 Beta 2 in VS 2005 and am running into a small problem. I need to be able to add a picture box to the main form from within a thread. The program goes to a web...
3
3657
by: Lonewolf | last post by:
Hi all, I'm having difficulties passing data back to managed class from my native class when the data is generated from within a native thread in the native class itself. I will give the following...
3
4684
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC...
2
1593
by: machado | last post by:
Greetings. I have a program in VB2008 and I need to execute a thread from differents Subs. Each time I start the thread I want to cancel the previous. Here an example: Private Sub...
0
7242
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
7353
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7418
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7075
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
7508
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...
0
5662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3212
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.