■ 콘솔 애플리케이션 종료시 탐지하는 방법을 보여준다.
▶ ControlType.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
namespace TestProject { /// <summary> /// 컨트롤 타입 /// </summary> public enum ControlType { /// <summary> /// CTRL_C_EVENT /// </summary> CTRL_C_EVENT = 0, /// <summary> /// CTRL_BREAK_EVENT /// </summary> CTRL_BREAK_EVENT = 1, /// <summary> /// CTRL_CLOSE_EVENT /// </summary> CTRL_CLOSE_EVENT = 2, /// <summary> /// CTRL_LOGOFF_EVENT /// </summary> CTRL_LOGOFF_EVENT = 5, /// <summary> /// CTRL_SHUTDOWN_EVENT /// </summary> CTRL_SHUTDOWN_EVENT = 6 } } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
using System; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Delegate ////////////////////////////////////////////////////////////////////////////////////////// Private #region 콘솔 컨트롤 핸들러 설정하기 대리자 - SetConsoleCtrlHandlerDelegate(controlType) /// <summary> /// 콘솔 컨트롤 핸들러 설정하기 대리자 /// </summary> /// <param name="controlType">컨트롤 타입</param> /// <returns>처리 결과</returns> private delegate bool SetConsoleCtrlHandlerDelegate(ControlType controlType); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 콘솔 컨트롤 핸들러 설정하기 - SetConsoleCtrlHandler(handler, add) /// <summary> /// 콘솔 컨트롤 핸들러 설정하기 /// </summary> /// <param name="handler">핸들러</param> /// <param name="add">추가 여부</param> /// <returns>처리 결과</returns> [DllImport("kernel32")] private static extern bool SetConsoleCtrlHandler(SetConsoleCtrlHandlerDelegate handler, bool add); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 콘솔 컨트롤 핸들러 - ConsoleCtrlHandler(controlType) /// <summary> /// 콘솔 컨트롤 핸들러 /// </summary> /// <param name="controlType">컨트롤</param> /// <returns>처리 결과</returns> private static bool ConsoleCtrlHandler(ControlType controlType) { switch(controlType) { case ControlType.CTRL_BREAK_EVENT : case ControlType.CTRL_C_EVENT : case ControlType.CTRL_LOGOFF_EVENT : case ControlType.CTRL_SHUTDOWN_EVENT : case ControlType.CTRL_CLOSE_EVENT : Environment.Exit(0); return false; default : return false; } } #endregion #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { SetConsoleCtrlHandler(ConsoleCtrlHandler, true); Console.WriteLine("프로그램을 종료하기 위해서 아무 키나 눌러 주시기 바랍니다."); Console.ReadKey(true); } #endregion } } |