我获得了MDI应用程序并且有一些OnFormClosing事件在End应用程序之前做了一些工作 - 那些行:
Projekt.UnlockAllLockedProjects()
End 'completly close app
低于整个代码:
Protected Overrides Sub OnFormClosing(e As FormClosingEventArgs)
MyBase.OnFormClosing(e)
If e.CloseReason = CloseReason.WindowsShutDown Then
Return
End If
Dim dr As DialogResult
Select Case Lang.name
Case Lang.LangShortcut.PL
dr = MessageBox.Show(Me, "Kontynuować zamykanie okna?", MsgLevel.QUESTION.ToString, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
Case Lang.LangShortcut.EN
dr = MessageBox.Show(Me, "Are you sure you want to quit?", MsgLevel.ZAPYTANIE.ToString, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
Case Else
End Select
' Confirm user wants to close
Select Case dr
Case DialogResult.No
e.Cancel = True
Exit Select
Case Else
Projekt.UnlockAllLockedProjects()
End 'completly close app
End Select
End Sub
问题是,让我们假设会出现应用程序错误,因此应用程序将崩溃或手动将其放在代码End
子句中的其他位置。如何确保在app退出之前始终执行Projekt.UnlockAllLockedProjects()
。有什么地方可以说出来并确定吗?
答案 0 :(得分:0)
我会在一个模块中使用Sub Main来显示GUI并最终在Try ... Catch中处理解锁过程。
示例:
Module Program
Public Sub Main(ByVal cmdArgs() As String)
Try
Application.Run(frmLogin)
Catch ex As Exception
'Any special code for handling exceptions goes here.
Finally
Projekt.UnlockAllLockedProjects()
End Try
End Sub
End Module