How can I get a closing event in this very simple app? It is a windowless winforms app. I do not want to have a tray icon, at least not a visible one.
Module Module1
Sub Main()
While True
MsgBox("I am still alive!")
System.Threading.Thread.Sleep(10000)
End While
End Sub
' how do I call this?
Public Sub ProgramClosing()
MsgBox("Program is closing. Good Bye.")
End Sub
End Module
UPDATE:
The code below works, but for some reason I get two messages "Program closing. Good Bye.", instead of one.
Module Module1
Sub Main()
AddHandler Application.ApplicationExit, AddressOf AppClosing
For i = 0 To 2
MsgBox(i & vbCrLf & "I am still alive!")
System.Threading.Thread.Sleep(5000)
Next
Application.Exit()
End Sub
Private Sub AppClosing(sender As Object, e As EventArgs)
MsgBox("Program closing. Good Bye.") ' THIS IS SHOWN TWICE
End Sub
End Module
UPDATE #2
I have tested to see if this event would fire when Windows actually shut down, and it did not.
I built the app with the code below, started the app (outside of Visual Studio), confirmed that it was running using Task Manager and turned off the computer. File "DebugFile.txt" never got created.
Module Module1
Sub Main()
AddHandler Application.ApplicationExit, AddressOf AppClosing
While True
' do tasks here
System.Threading.Thread.Sleep(10000)
End While
End Sub
Private Sub AppClosing(sender As Object, e As EventArgs)
RemoveHandler Application.ApplicationExit, AddressOf AppClosing
System.IO.File.AppendAllText("DebugFile.txt", "App closed at " & Now.ToString & vbCrLf)
End Sub
End Module