views:

1118

answers:

4

Using the .Net Framework 1.1, what options are available for monitoring threads from other threads? I am attempting to deal with a shortcoming of the threading implementation in 1.1 wherein unhandled exceptions will cause threads to die silently. In 2.0 and later this had been corrected so that any unhandled exception on any thread will cause the entire application to die, I beleive.

When threads in my application die, I would like to retrieve as much context as possible from the main thread: the method they were executing, a stack trace, etc. I can determine when they die but retrieving context has proven difficult. I have tried registering a handler for the AppDomain.CurrentDomain.UnhandledException event. But I am not getting called back when events occur. This is probably due to a limitation with the API I'm developing for.

Besides this event, what options are available to get context from threads as they die on another thread?

A: 

How about an incrementing count for each thread in a map indexed by the thread ID? When it stops incrementing, it's dead and time to kill and/or restart it. The map can contain everything you need to manage all of that as well and sharing info between the hosting thread and those nasty little children.

kenny
I can tell when the threads die by checking the Thread.IsAlive or Thread.ThreadState properties. The problem I'm having is detecting what was happening in the threads when they died, after the fact, from another thread.
Chris Stevens
Sounds like a job for log files.
kenny
+4  A: 

How are you creating these threads? Are you adding a delegate to the thread pool? IF so, you could create a wrapper method that takes the delegate provided and wraps another delegate around it which takes care of your try / catch, and then adds that new delegate to the thread pool queue. That way you could put in your error handling code in that second delegate.

John Christensen
That is one of the most clever ideas I have heard in a while. If there is no other way, I may have to do something like this.
Chris Stevens
A: 

I would create a method which will take the callback you would normally pass to a thread and then have that called inside some wrapper code which you would use to track exceptions in the thread, something like this:

public class ThreadStarter
{
  ThreadStart start;

  public ThreadStarter(ThreadStart start)
  {
    this.start = start
  }

  public void Run()
  {
    // Create the thread.
    Thread t = new Thread(new ThreadStart(InternalRun));
    t.Start();
  }

  private void InternalRun()
  {
    // Wrap in a try/catch.
    try
    {
      // Run the code.
      start();
    }
    catch (Exception e)
    {
      // Process exception here.
    }
  }
}
casperOne
+1  A: 

You could try add a thread exception handler:

The System.Threading.ThreadExceptionEventArgs e will contain the information about the unhandled exception.

// Setup Default Thread Exception Handler
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);


static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
    // Insert Code
}

Source: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.threadexception.aspx

VBNight