views:

290

answers:

3

Hello!

I have a dialog that has to process large quantaties of data (the procedure is quite time consuming - first the ODBC fill takes its time, then the data processing kicks in) and the result is that the Form becomes unresponsive. This is not a problem really, it is enough to just open a "loading screen" in a new thread to notify the user of the process.

Recently I have discovered, that sometimes (it appears to be random) the new thread will throw an unhandled ThreadAbortException causing a crash report dialog box to show up (or JIT).

I do not understand why this exception would be thrown, or why it would be unhandled. Has anyone dealt with this before, or can anyone point me towards the probable cause of this behaviour?

Thank you!

EDIT: In case it matters, I open the loading screen like this:

//start of work load
Thread th = new Thread(new ThreadStart(MakeStep));    
th.Start();

...
//end of work or error occurance:
th.Abort();

//

A: 

ThreadAbortExceptions are raised when you call Thread.Abort(). Looks like you or a lib you are using is trying to kill your Thread.

Jan Bannister
+2  A: 

You're calling th.Abort() which injects a ThreadAbortException on the thread th. If that thread doesn't handle the exception it will be reported as an unhandled exception.

It is generally not recommended to abort other threads in this way, as you have no idea if the thread will handle an abort gracefully. A better solution is to use signaling between your threads.

Brian Rasmussen
+1 you should link to some demo signaling code sample. asynchronous exceptions are evil
Sam Saffron
A: 

If you don't know why it's aborting, better to let it go than to swallow the exception.

That being said, you need to wrap your MakeStep method in a try/catch and log the exception (and, of course, any innter exceptions). Something like this...

public void MakeStep()
{
  try
  {
    InnerMakeStep(); // may throw TAE or some other exception
  }catch(Exception e)
  {
    // log here k 
    throw; // in case it isn't a TAE
  }
}

With the exception logged, the issue can be debugged. Right now, it could be a thousand things.

Will