static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ThreadException += (System.Threading.ThreadExceptionEventHandler)delegate(object sender, System.Threading.ThreadExceptionEventArgs e)
{
//这里处理线程异常
};
AppDomain.CurrentDomain.UnhandledException += (UnhandledExceptionEventHandler)delegate(object sender, UnhandledExceptionEventArgs e)
{
//这里处理其他未捕获的异常信息
};
Application.Run(new Mainform());
}
catch(Exception ex)
{
//这里处理标准异常
}
}
}
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ThreadException += (System.Threading.ThreadExceptionEventHandler)delegate(object sender, System.Threading.ThreadExceptionEventArgs e)
{
//这里处理线程异常
};
AppDomain.CurrentDomain.UnhandledException += (UnhandledExceptionEventHandler)delegate(object sender, UnhandledExceptionEventArgs e)
{
//这里处理其他未捕获的异常信息
};
Application.Run(new Mainform());
}
catch(Exception ex)
{
//这里处理标准异常
}
}
}
本文详细介绍了C#应用中使用STAThread特性进行主线程优化,并通过Application.Run和异常处理机制确保应用程序的稳定运行。重点阐述了如何设置线程异常事件处理器和全局未处理异常处理器,以有效管理应用内部的错误情况。
624

被折叠的 条评论
为什么被折叠?



