C # implement console Exit Control favorites
Using system;
Using system. runtime. interopservices;
Using system. Threading;
Using system. diagnostics; namespace leleapplication3
...{
Public Delegate bool consolectrldelegate (INT dwctrltype );
/** // <Summary>
/// Summary of class1.
/// </Summary>
Class class1
...{
[Dllimport ("kernel32.dll")]
Private Static extern bool setconsolectrlhandler (consolectrldelegate handlerroutine, bool add );
// A Ctrl + C message is received. The message may be sent from the primary or generateconsolectrlevent letter.
Private const int ctrl_c_event = 0;
// A Ctrl + break message is received. The message may come from the primary or generateconsolectrlevent letter.
Private const int ctrl_break_event = 1;
// When the guest system is used to shut down the console, the system will send this email to this
Private const int ctrl_close_event = 2;
// When the user exits the system, the system sends this email to all the console programs. The message cannot indicate which user exits.
Private const int ctrl_logoff_event = 5;
// The system will send this email to all the console programs when the system is about to shut down
Private const int ctrl_shutdown_event = 6;
/** // <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main (string [] ARGs)
...{
//
// Todo: Add code here to start the application
//
Class1 Cl = new class1 ();
} Public class1 ()
...{
Consolectrldelegate newdategate = new consolectrldelegate (handlerroutine );
Bool Re = setconsolectrlhandler (newdategate, true );
If (re)
...{
Console. writeline ("set setconsolectrlhandler success !! ");
}
Else
...{
Debug. writeline ("set setconsolectrlhandler Error !! ");
// Asreportfile. writefile ("", "test.txt", "who close? ");
}
Console. Readline ();
} Bool handlerroutine (INT ctrltype)
...{
Console. writeline ("set setconsolectrlhandler success !! ");
Console. writeline ("set setconsolectrlhandler success !! ");
Console. writeline ("set setconsolectrlhandler success !! ");
Console. writeline ("set setconsolectrlhandler success !! ");
Console. writeline ("set setconsolectrlhandler success !! ");
Console. writeline ("set setconsolectrlhandler success !! ");
Console. writeline ("set setconsolectrlhandler success !! "); Switch (ctrltype)
...{
Case ctrl_close_event:
For (INT I = 0; I <100; I ++)
...{
Console. writeline ("I is: {0}", I );
Thread. Sleep (1000 );
}
Break;
} Return false;
}
}
} This article from the csdn blog, reprint please indicate the source: http://blog.csdn.net/pojianbing/archive/2008/03/31/2234138.aspx
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