- 1 year ago
Suspend Windows Shutdown until application finished it´s work VB.Net
To suspend Windows shutdown until your application finishes its work in VB.NET, you can handle the Microsoft.Win32.SystemEvents.SessionEnding
event and cancel the shutdown process if needed. Here's an example:
vbImports Microsoft.Win32
Module Module1
Sub Main()
' Add the SessionEnding event handler
AddHandler SystemEvents.SessionEnding, AddressOf SystemEvents_SessionEnding
' Perform your application's work here
' ...
' Wait for the work to complete
' ...
' Remove the SessionEnding event handler
RemoveHandler SystemEvents.SessionEnding, AddressOf SystemEvents_SessionEnding
End Sub
Private Sub SystemEvents_SessionEnding(ByVal sender As Object, ByVal e As SessionEndingEventArgs)
' Check if the reason for session ending is shutdown
If e.Reason = SessionEndReasons.SystemShutdown Then
' Cancel the shutdown process
e.Cancel = True
End If
End Sub
End Module
In the above example, the SessionEnding
event is handled using the SystemEvents.SessionEnding
event handler. Inside the event handler, the reason for the session ending is checked. If the reason is a system shutdown (SessionEndReasons.SystemShutdown
), the shutdown process is canceled by setting e.Cancel
to True
.
Make sure to place your application's work inside the Main
method and ensure that the work completes before reaching the point of removing the event handler.
Please note that canceling a shutdown process should be done cautiously, as it can prevent Windows from shutting down properly. Make sure your application handles any necessary cleanup or data saving before suspending the shutdown process.