Cancelling My Thread (from within)

  • Thread starter Thread starter jp2msft
  • Start date Start date
J

jp2msft

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.
 
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?
 
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!
 
Peter Duniho said:
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?
 
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
 
Back
Top