-
Notifications
You must be signed in to change notification settings - Fork 14
/
Program.cs
117 lines (99 loc) · 3.66 KB
/
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.ServiceProcess;
using NLog;
using Exceptionless;
namespace WindowsServiceTemplate
{
static class Program
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private static Service _service;
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
ExceptionlessClient.Default.Register();
AppSettings.Init();
// self service installer/uninstaller
if (args != null && args.Length == 1
&& (args[0][0] == '-' || args[0][0] == '/'))
{
switch (args[0].Substring(1).ToLower())
{
case "install":
case "i":
if (!ServiceInstallerUtility.InstallMe())
Logger.Fatal("Failed to install service");
break;
case "uninstall":
case "u":
if (!ServiceInstallerUtility.UninstallMe())
Logger.Fatal("Failed to uninstall service");
break;
default:
Logger.Error("Unrecognized parameters (allowed: /install and /uninstall, shorten /i and /u)");
break;
}
Environment.Exit(0);
}
_service = new Service();
var servicesToRun = new ServiceBase[] { _service };
// console mode
if (Environment.UserInteractive)
{
// register console close event
_consoleHandler = ConsoleEventHandler;
SetConsoleCtrlHandler(_consoleHandler, true);
Console.Title = AppDomain.CurrentDomain.FriendlyName;
Logger.Debug("Running in console mode");
_service.Start();
Console.WriteLine("Press any key to stop the service...");
Console.Read();
_service.Stop();
}
// service mode
else
{
Logger.Debug("Running in service mode");
ServiceBase.Run(servicesToRun);
}
}
#region Page Event Setup
enum ConsoleCtrlHandlerCode : uint
{
// ReSharper disable InconsistentNaming
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT = 1,
CTRL_CLOSE_EVENT = 2,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT = 6
// ReSharper restore InconsistentNaming
}
delegate bool ConsoleCtrlHandlerDelegate(ConsoleCtrlHandlerCode eventCode);
[DllImport("kernel32.dll")]
static extern bool SetConsoleCtrlHandler(ConsoleCtrlHandlerDelegate handlerProc, bool add);
static ConsoleCtrlHandlerDelegate _consoleHandler;
#endregion
#region Page Events
static bool ConsoleEventHandler(ConsoleCtrlHandlerCode eventCode)
{
// Handle close event here...
switch (eventCode)
{
case ConsoleCtrlHandlerCode.CTRL_C_EVENT:
case ConsoleCtrlHandlerCode.CTRL_CLOSE_EVENT:
case ConsoleCtrlHandlerCode.CTRL_BREAK_EVENT:
case ConsoleCtrlHandlerCode.CTRL_LOGOFF_EVENT:
case ConsoleCtrlHandlerCode.CTRL_SHUTDOWN_EVENT:
_service.Stop();
Environment.Exit(0);
break;
}
return (false);
}
#endregion
}
}