프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / ConvertService / ServiceBase / Markus.Service.StationConsole / Program.cs @ 43e1d368

이력 | 보기 | 이력해설 | 다운로드 (1.75 KB)

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Runtime.InteropServices;
5
using System.Text;
6
using System.Threading.Tasks;
7

    
8
namespace Markus.Service.StationConsole
9
{
10
    class Program
11
    {   
12
        // https://msdn.microsoft.com/en-us/library/windows/desktop/ms686016.aspx
13
        [DllImport("Kernel32")]
14
        private static extern bool SetConsoleCtrlHandler(SetConsoleCtrlEventHandler handler, bool add);
15

    
16
        // https://msdn.microsoft.com/en-us/library/windows/desktop/ms683242.aspx
17
        private delegate bool SetConsoleCtrlEventHandler(CtrlType sig);
18
        private enum CtrlType
19
        {
20
            CTRL_C_EVENT = 0,
21
            CTRL_BREAK_EVENT = 1,
22
            CTRL_CLOSE_EVENT = 2,
23
            CTRL_LOGOFF_EVENT = 5,
24
            CTRL_SHUTDOWN_EVENT = 6
25
        }
26

    
27

    
28
        static Markus.Service.ServiceStation service;
29

    
30
        static void Main(string[] args)
31
        {
32
            SetConsoleCtrlHandler(Handler, true);
33
            service = new Markus.Service.ServiceStation();
34
            service.StartService();
35

    
36
            while (true)
37
            {
38
                System.Threading.Thread.Sleep(10000);
39
            }
40
        }
41

    
42
        private static bool Handler(CtrlType signal)
43
        {
44
            switch (signal)
45
            {
46
                case CtrlType.CTRL_BREAK_EVENT:
47
                case CtrlType.CTRL_C_EVENT:
48
                case CtrlType.CTRL_LOGOFF_EVENT:
49
                case CtrlType.CTRL_SHUTDOWN_EVENT:
50
                case CtrlType.CTRL_CLOSE_EVENT:
51

    
52
                    service.StopService();
53

    
54
                    // TODO Cleanup resources
55
                    Environment.Exit(0);
56
                    return false;
57

    
58
                default:
59
                    return false;
60
            }
61
        }
62
    }
63
}
클립보드 이미지 추가 (최대 크기: 500 MB)