Trap unhandled exception in windows application.
Best problem of windows desktop application is multithreading, and if multithreading code is incorrectly, for example marshaling data from background thread to frontend winform thread, windows application is disappear without any error message.
In this case I start desktop MDI WinForms application. And firstly need to set start point of application.
So, Below I show my real starter code of win apps with trap of exception in other thread.
1: Imports System.Security.Permissions
2: Imports System.Threading
3: Imports System.Runtime.InteropServices
4:
5:
6: Public Module Starter
7:
8: <DllImport("user32.dll")>
9: Private Function ShowWindow(ByVal hWnd As IntPtr, ByVal nCmdShow As Integer) As Boolean
10: End Function
11:
12: <DllImport("Kernel32")>
13: Private Function GetConsoleWindow() As IntPtr
14: End Function
15:
16: Const SW_HIDE As Integer = 0
17: Const SW_SHOW As Integer = 5
18:
19: Public frmCat_instance As frmCat
20: Public frmCatList_instance As frmCatList
21: Public frmCoChang_instance As frmCoChang
22: Public frmCoList_instance As frmCoList
23: Public frmCost_instance As frmCost
24: Public frmExport_instance As frmExport
25: Public frmGoJoin_instance As frmGoJoin
26: Public frmGoList_instance As frmGoList
27: Public frmGood_instance As frmGood
28: Public frmHiList_instance As frmHiList
29: Public frmImport_instance As frmImport
30: Public frmLogin_instance As frmLogin
31: Public frmMain_instance As frmMain
32: Public frmMaList_instance As frmMaList
33: Public frmMarket_instance As frmMarket
34: Public frmPassCng_instance As frmPassCng
35: Public frmProList_instance As frmProList
36: Public frmProvider_instance As frmProvider
37: Public frmRaList_instance As frmRaList
38: Public frmReport_instance As frmReport
39: Public frmSett_instance As frmSett
40: Public frmSpec_instance As frmSpec
41: Public frmSpList_instance As frmSpList
42: Public frmUser_instance As frmUser
43: Public frmUsList_instance As frmUsList
44: Public frmWelcome_instance As frmWelcome
45:
46:
47: <SecurityPermission(SecurityAction.Demand, Flags:=SecurityPermissionFlag.ControlAppDomain)>
48: Public Sub Main(ByVal args As String())
49:
50: Dim hwnd As IntPtr
51: hwnd = GetConsoleWindow()
52: ShowWindow(hwnd, SW_HIDE)
53:
54: AddHandler Application.ThreadException, New ThreadExceptionEventHandler(AddressOf Form1_UIThreadException)
55: Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException)
56: AddHandler AppDomain.CurrentDomain.UnhandledException, New UnhandledExceptionEventHandler(AddressOf CurrentDomain_UnhandledException)
57:
58: frmCat_instance = New frmCat
59: frmCatList_instance = New frmCatList
60: frmCoChang_instance = New frmCoChang
61: frmCoList_instance = New frmCoList
62: frmCost_instance = New frmCost
63: frmExport_instance = New frmExport
64: frmGoJoin_instance = New frmGoJoin
65: frmGoList_instance = New frmGoList
66: frmGood_instance = New frmGood
67: frmHiList_instance = New frmHiList
68: frmImport_instance = New frmImport
69: frmLogin_instance = New frmLogin
70: frmMain_instance = New frmMain
71: frmMaList_instance = New frmMaList
72: frmMarket_instance = New frmMarket
73: frmPassCng_instance = New frmPassCng
74: frmProList_instance = New frmProList
75: frmProvider_instance = New frmProvider
76: frmRaList_instance = New frmRaList
77: frmReport_instance = New frmReport
78: frmSett_instance = New frmSett
79: frmSpec_instance = New frmSpec
80: frmSpList_instance = New frmSpList
81: frmUser_instance = New frmUser
82: frmUsList_instance = New frmUsList
83: frmWelcome_instance = New frmWelcome
84:
85:
86: Application.Run(frmMain_instance)
87: End Sub
88:
89:
90: Private Sub Form1_UIThreadException(ByVal sender As Object, ByVal t As ThreadExceptionEventArgs)
91: Dim result As DialogResult = DialogResult.Cancel
92:
93: Try
94: result = ShowThreadExceptionDialog("Fatal Error", t.Exception)
95: If result = DialogResult.Abort Then Application.[Exit]()
96: Catch
97: Try
98: MessageBox.Show("Fatal Windows Forms Error", "UIThreadException", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.[Stop])
99: Finally
100: Application.[Exit]()
101: End Try
102: End Try
103:
104: Try
105: Dim ex As Exception = CType(t.Exception, Exception)
106: Dim DB1 As New BravoDB
107: DB1.errors.Add(New _error With {._date = Now, .error1 = GetMyIP().ToString & vbLf & ex.Message & vbLf + ex.StackTrace})
108: DB1.SaveChanges()
109:
110: Catch ex As Exception
111:
112: Try
113: MessageBox.Show("Sending error to server impossible", "UIThreadException", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.[Stop])
114: Finally
115: Application.[Exit]()
116: End Try
117: End Try
118:
119: End Sub
120:
121: Private Sub CurrentDomain_UnhandledException(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
122: Dim errorMsg As String = "Fatal Non-UI Error:" & vbLf & vbLf
123: Dim ex As Exception = CType(e.ExceptionObject, Exception)
124: Try
125:
126: Dim DB1 As New BravoDB
127: DB1.errors.Add(New _error With {._date = Now, .error1 = GetMyIP().ToString & vbLf & ex.Message & vbLf + ex.StackTrace})
128: DB1.SaveChanges()
129:
130: Catch exc As Exception
131: MessageBox.Show("Sending error to server impossible", "UnhandledException", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.[Stop])
132: Finally
133: Application.[Exit]()
134: End Try
135:
136: Try
137: Dim myLog As EventLog = New EventLog()
138: myLog.Source = "ThreadException"
139: myLog.WriteEntry(errorMsg & ex.Message & vbLf & vbLf & "Stack Trace:" & vbLf + ex.StackTrace)
140: Catch exc As Exception
141:
142: Try
143: MessageBox.Show("Could not write the error to the event log. Reason: " & exc.Message, "UnhandledException", MessageBoxButtons.OK, MessageBoxIcon.[Stop])
144: Finally
145: Application.[Exit]()
146: End Try
147: End Try
148: End Sub
149:
150: Public Function ShowThreadExceptionDialog(ByVal title As String, ByVal e As Exception) As DialogResult
151: Dim errorMsg As String = "An application error occurred. Please contact the adminstrator " & "with the following information:" & vbLf & vbLf
152: errorMsg = errorMsg & e.Message & vbLf & vbLf & "Stack Trace:" & vbLf + e.StackTrace
153: Return MessageBox.Show(errorMsg, title, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.[Stop])
154: End Function
155:
156: Public Function GetMyIP() As Net.IPAddress
157: Try
158: Using wc As New Net.WebClient
159: Return Net.IPAddress.Parse(Text.Encoding.ASCII.GetString(wc.DownloadData("http://tools.feron.it/php/ip.php")))
160: End Using
161: Catch ex As Exception
162: Return New Net.IPAddress(0)
163: End Try
164:
165: End Function
166:
167: End Module
This code show black screen of console application to short time and after that this windows is disappear and appear a main window of winforms application.
Additionally my code collect application error to server.
More example to use this starter Multithreading Parsers with Parallel, CsQuery, Newtonsoft.Json, OfficeOpenXml and IAsyncResult/AsyncCallback.
<SITEMAP> <MVC> <ASP> <NET> <DATA> <KIOSK> <FLEX> <SQL> <NOTES> <LINUX> <MONO> <FREEWARE> <DOCS> <ENG> <CHAT ME> <ABOUT ME> < THANKS ME> |