I have an application that starts from Sub Main and I need to run a sub when the system is about to shut down, restart or logoff so it can save it's settings. I found some code on google:
But it doesn't seem to work, Windows shuts down yes but when I re-start the vm and run the app it opens with the old settings, not the ones that I changed before I shut down the vm. Anyone know of a way to do this or to fix the code above, I don't think my OnShuttingdown event is being handled.
VB.NET:
Imports Microsoft.Win32
Public Sub Main
AddHandler SystemEvents.SessionEnding, AddressOf OnShuttingdown
'Rest of app, including the While Loop to do it's thing
End Sub
Public Sub OnShuttingdown(ByVal sender As Object, ByVal e As SessionEndingEventArgs)
'Code that signal's app's exiting which in turn sets a variable that tells the While Loop to exit
End Sub