473,586 Members | 2,681 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.Cou nt == 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 1651
On Aug 15, 11:05*pm, jp2msft <jp2m...@discus sions.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.Cou nt == 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*****@discus sions.microsoft .comwrote:
[...]
How do I exit the thread? I have tried:
/*************** **************/
if (table.Rows.Cou nt == 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_RunWorke rCompleted 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.Cou nt == 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*****@discus sions.microsoft .comwrote:
Solved it myself, actually.

Every time my Thread_RunWorke rCompleted 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 RunWorkerComple ted event
*can* read this back in?
Aug 15 '08 #6
On Aug 15, 10:05*pm, jp2msft <jp2m...@discus sions.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.Cou nt == 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*****@discus sions.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 RunWorkerComple ted
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
1872
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. One of the main problems at this point lies in cancelling the event. I have found that the TAB fires the onkeypress in NN, but not in IE. I can...
16
1358
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. That would enable the simple test: var ref = new someObject(); if( ref ) { // Object created successfully }
1
7338
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 each line of a file, processes it , and writes it. With the Cancel button, I'd like to cancel this method. Is this possible? Thanks.
7
2679
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 a lock pending, for example. I want to be able to stop a thread temporarily, and then optionally resume it or stop it for good.
0
879
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 BeginInvoke on a delegate? I suspect that there isn't, but have been unable to confirm this suspicion. Thanks.
3
3682
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 site, performs a search based on POST variables, retrieves the code, parses the information (including a url for a thumbnail image) and displays the...
3
3663
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 runtime error, " Attempting to call into managed code without transitioning out first. Do not attempt to run managed code inside low-level native...
3
4699
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 application. Furthermore, I use a pointer to an unmanaged function to jump back into the managed dll. The managed part is basically a remoting...
2
1599
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 TextBox_Changed(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim ThreadResults As New Thread(AddressOf ShowResults) ...
0
7912
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8202
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3837
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2345
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 we have to send another system
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.