我的Windows应用程序入口点是Main窗体构造函数。当应用程序启动时,它将启动一个线程,该线程保持运行状态。几天后,我的应用程序崩溃,提示应用程序弹出窗口停止工作。
public MDIForm()
{
InitializeComponent();
AppDomain.CurrentDomain.UnhandledException += (s, e) =>
{
Exception ex = (Exception)e.ExceptionObject;
System.Diagnostics.Debug.WriteLine(ex.Message);
logtofile"MDIForm; Error; UnhandledException;" + ex.Message);
logtofile("MDIForm" + " StackTrace: UnhandledException: '{0}'" + Environment.StackTrace);
};
Application.ThreadException += (s, ex) =>
{
Exception exc = (Exception)ex.Exception;
System.Diagnostics.Debug.WriteLine(exc.Message);
logtofile("MDIForm; Error; ThreadException; " + exc.Message);
logtofile("MDIForm" + " StackTrace: ThreadException: '{0}'" + Environment.StackTrace);
};
}
以下功能在应用程序启动时从线程中调用。
private void MyFunction_RunThread()
{
try
{
while (true)
{
//My Code
Application.DoEvents();
Thread.Sleep(200);
}
}
catch (Exception Ex)
{
//My Code
}
}
在添加了Stacetrace的代码之后,将捕获以下参考日志。
MDIForm StackTrace: UnhandledException: '{0}' at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
at System.Environment.get_StackTrace()
at MyApp.MDIForm.<.ctor>b__0(Object s, UnhandledExceptionEventArgs e)
at System.Windows.Forms.UnsafeNativeMethods.IntCreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
at System.Windows.Forms.UnsafeNativeMethods.CreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
at System.Windows.Forms.Control.CreateHandle()
at System.Windows.Forms.Application.MarshalingControl..ctor()
at System.Windows.Forms.Application.ThreadContext.get_MarshalingControl()
at System.Windows.Forms.WindowsFormsSynchronizationContext..ctor()
at System.Windows.Forms.WindowsFormsSynchronizationContext.InstallIfNeeded()
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.DoEvents()
at MyApp.MyClass.MyFunction_RunThread()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()