winforms应用程序意外崩溃 [英] winforms application unexpectedly crashes
问题描述
我有一个winforms应用程序(一个大的1 mdi和大约100多种形式)。我在applicationevents.vb中添加了代码来处理未处理的异常。
Hi,
I've a winforms applicatoin (a big one with 1 mdi and about 100+ forms). I've added code in applicationevents.vb to handle the unhandled exceptions.
Namespace My
Partial Friend Class MyApplication
Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
e.ExitApplication = False
IO.File.AppendAllText("error.txt", Format(Now, "dd-MMM-yyyy HH:mm:ss") & " - Unex - " & e.Exception.Message & vbCrLf & _
e.Exception.StackTrace & vbCrLf)
MessageBox.Show("got error")
End Sub
End Class
End Namespace
在这里,我可以获取消息框,并将异常详细信息写入txt文件。但是应用程序关闭了,即使我明确地将ExitApplication编码为False。
当我在一个简单的winform中测试场景时,它运行正常。但在这个项目中,我正面临着这个问题。有没有特别的配置我错过了?
非常感谢任何帮助/想法。
Here I'm able to get the message box, and the exception details written to the txt file. But the application closes, even though I explicitly coded ExitApplication to False.
When I test the scenario in a simple winform, it works fine. But in this project , I'm facing this problem. Is there any special configuration I'm missing out?
Any help/ idea greatly appreciated.
推荐答案
这不是你应该怎么做的。您需要处理应用程序的每个外部事件处理循环中的所有异常。为此,System.Windows.Forms.Application
类具有以下功能:
http://msdn.microsoft.com/en-us/library/system.windows。 forms.application.onthreadexception%28v = vs.110%29.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.threading.threadexceptioneventhandler%28v= vs.110%29.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.threading.threadexceptioneventargs%28v=vs.110%29.aspx [ ^ ],
另见 http:// msdn.microsoft.com/en-us/library/ms157905(v=vs.110).aspx [ ^ ],
或 http://msdn.microsoft.com/en-us/library/ms223898(v = vs.110)的.aspx [ ^ ]。
首先,在你的入口点方法(Main
)中,你需要设置这种模式:
This is not how you should do it. You need to handle all exceptions in every outer event handling loop of the application. For this purpose,System.Windows.Forms.Application
class has the following facility:
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.onthreadexception%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.threading.threadexceptioneventhandler%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.threading.threadexceptioneventargs%28v=vs.110%29.aspx[^],
see also http://msdn.microsoft.com/en-us/library/ms157905(v=vs.110).aspx[^],
or http://msdn.microsoft.com/en-us/library/ms223898(v=vs.110).aspx[^].
First of all, in your entry-point method (Main
), you need to set up this mode:
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException)
' see other values of this enum
然后,在某处为事件 Application.OnThreadException
安装一个异常处理程序。我通常在主窗体的构造函数中执行它,在开始时,只是在处理程序中使用一些窗体方法,但它可以在其他任何地方尽快完成。事件参数类 System.Threading.ThreadExceptionEventArgs
使您可以访问异常实例( ThreadExceptionEventArgs.Exception
)。示例代码可以在这里找到:
http://msdn.microsoft.com/en-us/library/system.threading.threadexceptioneventargs%28v=vs.110%29.aspx [ ^ ]。
And then, install an exception handler for the event Application.OnThreadException
somewhere. I usually do it in the constructor of the main form, in the very beginning, just to use some form methods in the handler, but it could be done anywhere else, as soon as possible. The event arguments class System.Threading.ThreadExceptionEventArgs
gives your the access to the exception instance (ThreadExceptionEventArgs.Exception
). The sample code can be found here:
http://msdn.microsoft.com/en-us/library/system.threading.threadexceptioneventargs%28v=vs.110%29.aspx[^].
在app.config文件中
In the app.config file
<system.windows.forms jitdebugging="true" />
我不记得我何时添加了这个。使其成为错误解决了问题。
I coundn't remember when I've added this. Making it to false solved the problem.
这篇关于winforms应用程序意外崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!