471,353 Members | 1,711 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,353 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 1565
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Paul THompson | last post: by
16 posts views Thread by Michael Winter | last post: by
7 posts views Thread by Charles Law | last post: by
reply views Thread by stand__sure | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.