TIPS: How can I send messages to my applications, console programs, and Windows Services when the operating system is canceled or restarted?

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.