How to get the name of the thread in which the exception occurred?

I handle thread exceptions, but I want to get the name of the thread in which the exception occurred. It appears that when the thread exception is triggered, the event remains in the main thread, although I think the exception could have occurred in another thread.

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

In static void Main ():

 Thread.CurrentThread.Name = "Main Thread"; 

VS 2010 shows the main thread as having the name "Main thread", but in fact the name of the thread is null.

+2
source

If you mean handling the Application.ThreadException event: it fires only for exceptions that were selected from WinForms threads. Typically, an application has one WinForms thread: the main thread.

UPDATE

Here's an example showing the difference in the behavior of Application.ThreadException and AppDomain.UnhandledException:

1) Program class:

 static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); Application.Run(new Form1()); } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { Debug.WriteLine(Thread.CurrentThread.Name); } static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) { Debug.WriteLine(Thread.CurrentThread.Name); } } 

2) The main form (form with two buttons) code-behind:

 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { throw new InvalidOperationException(); } private void button2_Click(object sender, EventArgs e) { new Thread(() => { throw new InvalidOperationException(); }) { Name = "Worker Thread" }.Start(); } } 

When you click on button1, you throw an exception from the WinForms thread. Thus, this exception will be handled by default Application_ThreadException .

When you click button2, you throw an exception to the workflow, which is not a WinForms thread. Application.ThreadException does not fire in this case; instead, the AppDomain.UnhandledException event is CurrentDomain_UnhandledException (and a CurrentDomain_UnhandledException is CurrentDomain_UnhandledException , producing the "Workflow" line in the output window).

+1
source

Use a variable numerical variable (e.g. byte) to give each name its name, e.g.

 string threadname = "Thread" + threadnumber 

And then use the catch statement to notify you like this:

 ShowFaultDialog(e.exception, threadname) 

Thus, you can determine in which stream it is, theoretically.

0
source

As I understand it from MSDN, the Application_ThreadException event allows Windows Forms applications to handle unhandled exceptions that occur in Windows Forms threads and when you reach this event, you will end up in the main user interface thread. Therefore, he will always print the same thing.

Have you checked the Exception.TargetSite property? This property returns the name of the method and the signature where the exception occurred.

0
source

All Articles