如何在 VB.Net 中禁用错误对话框
本文关键字:错误 对话框 Net VB | 更新日期: 2023-09-27 18:34:16
如果应用创建异常,Windows 将暂停并显示错误对话框。用户可以决定关闭应用。
如何禁用此框?(是的,我知道尝试,捕获,最后,但我想为应用程序全局禁用它。我想显示MyErrorForm.vb而不是标准错误框,或者禁用它。
我找到了答案!放入程序.cs(主文件)此代码:
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
(放在Application.Run
之前)和广告错误处理:
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
//TODO: Handle error...
}
你需要钩住:
-
AppDomain.UnhandledException
-
Application.ThreadException
有关详细信息,请参阅 Application.ThreadException 和 AppDomain.CurrentDomain.UnhandledException?
将代码模块文件添加到项目中,将其命名为 UnhandledExceptions.vb然后添加以下代码来替换默认内容。
Imports Microsoft.VisualBasic.ApplicationServices
Namespace My
Partial Friend Class MyApplication
''' <summary>
''' Indicates if we are running under the IDE or not
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public ReadOnly Property RunningUnderDebugger() As Boolean
Get
Return System.Diagnostics.Debugger.IsAttached
End Get
End Property
Private mUnhandledExceptionsFileName As String
Public Property UnhandledExceptionsFileName() As String
Get
Return mUnhandledExceptionsFileName
End Get
Set(ByVal value As String)
mUnhandledExceptionsFileName = value
End Set
End Property
Private mExceptionDialogIcon As Icon
''' <summary>
''' Specifically used to set the exception dialog's icon
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
Public Property ExceptionDialogIcon() As Icon
Get
Return mExceptionDialogIcon
End Get
Set(ByVal value As Icon)
mExceptionDialogIcon = value
End Set
End Property
Private mContinueAfterException As Boolean
''' <summary>
''' Determine if this app can stay open after an unhandled
''' exception has been thrown.
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks>
''' Not practical in most circumstances as we usually
''' do not know how to hangle unhandled exceptions and
''' leaving an app open will not be wise.
''' </remarks>
Public Property ContinueAfterException() As Boolean
Get
Return Not mContinueAfterException
End Get
Set(ByVal value As Boolean)
mContinueAfterException = value
End Set
End Property
''' <summary>
''' Displays a user friendly message in regards to the
''' unhandled exception.
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks>
''' It would be wise to also write to a log file etc.
'''
''' WARNING
''' If you use code prone to errors in here then you will not
''' be very happy so test test test before implementing.
''' </remarks>
Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs) Handles Me.UnhandledException
If String.IsNullOrEmpty(mUnhandledExceptionsFileName) Then
mUnhandledExceptionsFileName = "unHandledExceptions.xml"
End If
Dim Doc As New XDocument
If IO.File.Exists(My.Application.UnhandledExceptionsFileName) Then
Doc = XDocument.Load(My.Application.UnhandledExceptionsFileName)
Else
Doc = XDocument.Parse("<?xml version=""1.0"" encoding=""utf-8""?><Exceptions></Exceptions>")
End If
Dim Content = Doc.<Exceptions>(0)
Dim StackTraceText As String = e.Exception.StackTrace.ToString
Content.Add(
<Exception>
<Date_Time><%= Now.ToString("MM/dd/yyyy HH:mm") %></Date_Time>
<Message><%= e.Exception.Message %></Message>
<StackTrace><%= Environment.NewLine %>
<%= StackTraceText %><%= Environment.NewLine %>
</StackTrace>
</Exception>)
Content.Save(My.Application.UnhandledExceptionsFileName)
' here I am using a form setup specifically to show exceptions
'Dim f As New frmExceptionDialog
'f.InBoundException = e.Exception
'f.Message = String.Format("Exception has been recorded to [{0}]", My.Application.UnhandledExceptionsFileName)
'f.Header = "Unhandled exception"
'Try
' f.ShowDialog()
'Finally
' f.Dispose()
'End Try
MessageBox.Show("Encountered an exception" & Environment.NewLine & e.Exception.Message)
e.ExitApplication = Me.ContinueAfterException
If Me.ContinueAfterException Then
Me.ContinueAfterException = False
End If
End Sub
End Class
End Namespace
启动窗体中的示例用法
Public Class Form1
Private ExceptionLogFile As String = ""
Public Sub New()
InitializeComponent()
My.Application.UnhandledExceptionsFileName = "unHandledExceptions.xml"
ExceptionLogFile = My.Application.UnhandledExceptionsFileName
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
If My.Application.RunningUnderDebugger Then
MessageBox.Show("Please execute from Window's Explorer")
Application.ExitThread()
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Throw New Exception("Hey")
End Sub
End Class
生成的输出
<?xml version="1.0" encoding="utf-8"?>
<Exceptions>
<Exception>
<Date_Time>02/10/2016 15:45</Date_Time>
<Message>Hey</Message>
<StackTrace>
at WindowsApplication3.Form1.Button1_Click(Object sender, EventArgs e) in C:'Dotnet_Development'Projects_Non_Business'WindowsApplication3'Form1.vb:line 16
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
</StackTrace>
</Exception>
</Exceptions>
我使用了xml,但您可以轻松使用文本文件或其他类型的文件。当我对异常进行这种类型的工作时,代码也会向我发送电子邮件。
另请参阅哪个具有用于显示消息而不是消息框的自定义窗体。处理 Windows 表单解决方案中未经处理的异常
重要说明:在第一个文件中有一个属性,ContinueAfterException,这在 99% 的情况下设置为 false,因为将其设置为 true 将允许(如果可能)应用程序保持活动状态,但很可能不稳定。