473,236 Members | 1,550 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,236 software developers and data experts.

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 1633
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
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
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
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
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
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
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
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
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
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.