Search code examples
c#winformsthread-exceptions

How do I get the name of the thread that the exception occurred on?


I am handling thread exceptions but I want to get the name of the Thread that the exception occurred on. It appears that when the thread exception fires the event stays on the main thread although I think the exception could have occurred on a different thread.

static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
    ShowFaultDialog(e.Exception, "(Application) Thread Exception [" + System.Threading.Thread.CurrentThread.Name + "]");
}

Solution

  • In static void Main():

    Thread.CurrentThread.Name = "Main Thread";
    

    VS 2010 shows the main thread as having a 'Name' of "Main Thread" but actually the thread name is null.