本文整理匯總了C#中System.Threading.ThreadExceptionEventArgs.ThreadExceptionEventArgs構造函數的典型用法代碼示例。如果您正苦於以下問題:C# ThreadExceptionEventArgs構造函數的具體用法?C# ThreadExceptionEventArgs怎麽用?C# ThreadExceptionEventArgs使用的例子?那麽, 這裏精選的構造函數代碼示例或許可以為您提供幫助。您也可以進一步了解該構造函數所在類System.Threading.ThreadExceptionEventArgs
的用法示例。
在下文中一共展示了ThreadExceptionEventArgs構造函數的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: ErrorForm
//引入命名空間
using System;
using System.Threading;
using System.Windows.Forms;
// Create a form with a button that, when clicked, throws an exception.
public class ErrorForm : System.Windows.Forms.Form
{
internal Button button1;
public ErrorForm() : base()
{
// Add the button to the form.
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(100, 43);
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.Text = "Click!";
this.Controls.Add(this.button1);
this.button1.Click += new EventHandler(this.button1_Click);
this.Text = "ThreadException";
this.ResumeLayout(false);
}
// Throw an exception when the button is clicked.
private void button1_Click(object sender, System.EventArgs e)
{
throw new ArgumentException("The parameter was invalid");
}
public static void Main()
{
// Add the event handler.
Application.ThreadException += new ThreadExceptionEventHandler(CustomExceptionHandler.OnThreadException);
// Start the example.
Application.Run(new ErrorForm());
}
}
// Create a class to handle the exception event.
internal class CustomExceptionHandler
{
// Handle the exception event
public static void OnThreadException(object sender, ThreadExceptionEventArgs t)
{
DialogResult result = ShowThreadExceptionDialog(t.Exception);
// Exit the program when the user clicks Abort.
if (result == DialogResult.Abort)
Application.Exit();
}
// Create and display the error message.
private static DialogResult ShowThreadExceptionDialog(Exception e)
{
string errorMsg = "An error occurred. Please contact the adminstrator " +
"with the following information:\n\n";
errorMsg += String.Format("Exception Type: {0}\n\n", e.GetType().Name);
errorMsg += "\n\nStack Trace:\n" + e.StackTrace;
return MessageBox.Show(errorMsg, "Application Error",
MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
}
}