-
Notifications
You must be signed in to change notification settings - Fork 6
/
Program.vb
64 lines (51 loc) · 2.34 KB
/
Program.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
Class Program
''' <summary>
''' 应用程序的主入口点。
''' </summary>
''' <remarks></remarks>
<STAThread()> _
Public Shared Sub Main()
Try
'始终捕获异常(UI线程不会因为异常而直接结束)
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException)
Catch ex As InvalidOperationException
'多次执行Main函数无效(只启动一次窗体)
Return
End Try
AddHandler Application.ThreadException, AddressOf Application_ThreadException
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf CurrentDomain_UnhandledException
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
'设置启动窗体
Dim FormMain = New FormMain()
FormMain.Opacity = 0
FormMain.Hide()
Application.Run(FormMain)
End Sub
''' <summary>
''' 在这里处理UI线程异常,注意:函数执行完成后,应用程序仍会继续运行
''' </summary>
''' <remarks></remarks>
Private Shared Sub Application_ThreadException(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs)
MessageBox.Show("窗体UI线程发生异常," & DateTime.Now.ToString() & ":" & vbCrLf & e.Exception.ToString())
End Sub
''' <summary>
''' 在这里处理子线程异常,注意:函数执行完成后,应用程序就会被终止
''' </summary>
''' <remarks></remarks>
Private Shared Sub CurrentDomain_UnhandledException(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
MessageBox.Show("子线程发生异常," & DateTime.Now.ToString() & ":" & vbCrLf & e.ExceptionObject.ToString())
End Sub
''' <summary>
''' 测试异常捕获效果(触发一个UI线程异常和一个子线程异常)
''' </summary>
''' <remarks></remarks>
Public Shared Sub TestException()
Dim Temp As New Threading.Thread(New Threading.ThreadStart(AddressOf TestExceptionThread))
Temp.Start()
Throw New Exception("测试用的UI线程异常")
End Sub
Private Shared Sub TestExceptionThread()
Throw New Exception("测试用的子线程异常")
End Sub
End Class