Share via


ThreadExceptionEventArgs 类

ThreadException 事件提供数据。

**命名空间:**System.Threading
**程序集:**System(在 system.dll 中)

语法

声明
Public Class ThreadExceptionEventArgs
    Inherits EventArgs
用法
Dim instance As ThreadExceptionEventArgs
public class ThreadExceptionEventArgs : EventArgs
public ref class ThreadExceptionEventArgs : public EventArgs
public class ThreadExceptionEventArgs extends EventArgs
public class ThreadExceptionEventArgs extends EventArgs

备注

发生未处理的异常时,线程将创建一个 ThreadExceptionEventArgsThreadExceptionEventArgs 包含已发生的 Exception

示例

下面的示例允许您通过单击窗体上的 button1 来引发 ThreadException 事件。示例创建两个类。ErrorHandler 类创建引发事件的窗体和按钮。CustomExceptionHandler 类提供处理异常的方法。

ErrorHandler 类的 Main 中,这段代码创建异常处理类的新实例(即 CustomExceptionHandler 的实例)。然后该实例被添加到事件中,并且运行应用程序。

CustomExceptionHandler 类的 OnThreadException 方法中,此示例使用 try...catch...finally 语句处理异常。ShowThreadExceptionDialog 方法创建要显示的消息并在消息框中显示。

' Creates a class to throw the error.
Public Class ErrorHandler
    Inherits System.Windows.Forms.Form    
    
    ' Inserts code to create a form with a button.
    
    ' Programs the button to throw the exception when clicked.
    Private Sub button1_Click(sender As Object, e As System.EventArgs)
        Throw New ArgumentException("The parameter was invalid")
    End Sub
    
    Public Shared Sub Main()
        ' Creates an instance of the methods that will handle the exception.
        Dim eh As New CustomExceptionHandler()
        
        ' Adds the event handler to to the event.
        AddHandler Application.ThreadException, AddressOf eh.OnThreadException
        
        ' Runs the application.
        Application.Run(New ErrorHandler())
    End Sub
    
End Class

' Creates a class to handle the exception event.
Friend Class CustomExceptionHandler
    
    'Handles the exception event
    Public Sub OnThreadException(sender As Object, t As ThreadExceptionEventArgs)
        Dim result As DialogResult = DialogResult.Cancel
        Try
            result = Me.ShowThreadExceptionDialog(t.Exception)
        Catch
            Try
                MessageBox.Show("Fatal Error", "Fatal Error", _
                    MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop)
            Finally
                Application.Exit()
            End Try
        End Try
        
        ' Exits the program when the user clicks Abort.
        If result = DialogResult.Abort Then
            Application.Exit()
        End If
    End Sub
     
    ' Creates the error message and display it.
    Private Function ShowThreadExceptionDialog(e As Exception) As DialogResult
        Dim errorMsg As String = "An error occurred please contact the " & _
            "adminstrator with the following information:" & _
            Microsoft.VisualBasic.ControlChars.Cr & _
            Microsoft.VisualBasic.ControlChars.Cr
        errorMsg &= e.Message & Microsoft.VisualBasic.ControlChars.Cr & _
            Microsoft.VisualBasic.ControlChars.Cr & "Stack Trace:" & _
            Microsoft.VisualBasic.ControlChars.Cr & e.StackTrace
        Return MessageBox.Show(errorMsg, "Application Error", _
            MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop)
    End Function
End Class
// Creates a class to throw the error.
 public class ErrorHandler : System.Windows.Forms.Form {
 
    // Inserts code to create a form with a button.
 
    // Programs the button to throw the exception when clicked.
    private void button1_Click(object sender, System.EventArgs e) {
       throw new ArgumentException("The parameter was invalid");
    }
 
    public static void Main(string[] args) {
       // Creates an instance of the methods that will handle the exception.
       CustomExceptionHandler eh = new CustomExceptionHandler();
 
       // Adds the event handler to to the event.
       Application.ThreadException += new ThreadExceptionEventHandler(eh.OnThreadException);
 
       // Runs the application.
       Application.Run(new ErrorHandler());
    }
 }
 
 // Creates a class to handle the exception event.
 internal class CustomExceptionHandler {
 
    //Handles the exception event
    public void OnThreadException(object sender, ThreadExceptionEventArgs t) 
    {
       DialogResult result = DialogResult.Cancel;
       try {
          result = this.ShowThreadExceptionDialog(t.Exception);
       }
       catch {
          try {
             MessageBox.Show("Fatal Error", "Fatal Error", 
                MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
          }
          finally {
             Application.Exit();
          }
       }
 
       // Exits the program when the user clicks Abort.
       if (result == DialogResult.Abort) 
          Application.Exit();
    }
 
    // Creates the error message and display it.
    private DialogResult ShowThreadExceptionDialog(Exception e) {
       string errorMsg = "An error occurred please contact the adminstrator " +
            "with the following information:\n\n";
       errorMsg = errorMsg + e.Message + "\n\nStack Trace:\n" + e.StackTrace;
       return MessageBox.Show(errorMsg, "Application Error", 
            MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
    }
 }
 
// Creates a class to handle the exception event.
private ref class CustomExceptionHandler
{
public:

   //Handles the exception event
   void OnThreadException( Object^ /*sender*/, ThreadExceptionEventArgs^ t )
   {
      DialogResult result = DialogResult::Cancel;
      try
      {
         result = this->ShowThreadExceptionDialog( t->Exception );
      }
      catch ( Exception^ ) 
      {
         try
         {
            MessageBox::Show( "Fatal Error", "Fatal Error", MessageBoxButtons::AbortRetryIgnore, MessageBoxIcon::Stop );
         }
         finally
         {
            Application::Exit();
         }

      }

      
      // Exits the program when the user clicks Abort.
      if ( result == DialogResult::Abort )
            Application::Exit();
   }


private:

   // Creates the error message and display it.
   DialogResult ShowThreadExceptionDialog( Exception^ e )
   {
      String^ errorMsg = "An error occurred please contact the adminstrator with the following information:\n\n";
      errorMsg = String::Concat( errorMsg, e->Message, "\n\nStack Trace:\n", e->StackTrace );
      return MessageBox::Show( errorMsg, "Application Error", MessageBoxButtons::AbortRetryIgnore, MessageBoxIcon::Stop );
   }

};


// Creates a class to throw the error.
public ref class ErrorHandler: public System::Windows::Forms::Form
{
private:

   // Inserts code to create a form with a button.
   // Programs the button to throw the exception when clicked.
   void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      throw gcnew ArgumentException( "The parameter was invalid" );
   }

};

int main()
{
   
   // Creates an instance of the methods that will handle the exception.
   CustomExceptionHandler^ eh = gcnew CustomExceptionHandler;
   
   // Adds the event handler to to the event.
   Application::ThreadException += gcnew ThreadExceptionEventHandler( eh, &CustomExceptionHandler::OnThreadException );
   
   // Runs the application.
   Application::Run( gcnew ErrorHandler );
}

继承层次结构

System.Object
   System.EventArgs
    System.Threading.ThreadExceptionEventArgs
       Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

ThreadExceptionEventArgs 成员
System.Threading 命名空间
Thread 类
ThreadStart
ThreadExceptionEventHandler