For Windows form applicationsProgramThe operating system sends a message of wm_queryendsession to every form when it is canceled or closed. Each form has a voting opportunity and returns whether to exit. this is the same as the closing time of form. once a program returns false, the logout operation is canceled. default.. net.
Therefore, You need to log off the message from the corresponding system, for example, to remind the user whether to exit, or to perform some log record operations, in.. Net only needs to handle an event to Microsoft. win32.systemevents. the sessionending event goes above. you can perform some custom operations. for example:
Private Void Hooksessionending ()
{
Microsoft. win32.systemevents. sessionending+ =
NewMicrosoft. win32.sessionendingeventhandler (This. Onsessionending );
}
Private Void Onsessionending ( Object Sender,
Microsoft. win32.sessionendingeventargs E)
{
//The user session is ending
// MessageBox...
// Cancel system shutdown E. Cancel = true
}
Of course, you can also reload message processing and process the message wm_queryendsession by yourself.
For console applications, you can only passively accept them. You can record when the program is disabled. Why?
The implementation method is actually to call the system API. Some setconsole hitting functions are used to customize console applications, such as font, color, and form size subclass. in 2005, these APIs are actually packaged. setconsolectrlhandler is used to register a handler.
A framework of VB.net
Module module1 Module Module1
Public Declare Function setconsolectrlhandler () Function Setconsolectrlhandler Lib " Kernel32.dll " Alias " Setconsolectrlhandler " ( Byval Handlerroutine As Handlerroutine, Byval Add As Boolean ) As Boolean
Public Delegate Sub handlerroutine () Sub Handlerroutine ( Byval Type As Integer )
Sub main () SubMain ()
Setconsolectrlhandler (AddressofMyhandle,True)
Console. Readline ()
End sub
Public Sub myhandle () Sub Myhandle ( Byval Type As Integer )
' Messages received by the Console
' 0-CTL C
' 1-CTL + break
' 2-Close, either by closing the close button or directly by closing the process
' 3-log out of the operating system
' 4-system shutdown
' Add your log
End sub
End Module
3. For Windows Services, the API adopts a similar processing method as the console, except that its status switches more.
It mainly calls a registerservicectrlhandler API.