-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
87 lines (77 loc) · 2.82 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
using System;
using System.Collections.Generic;
using System.Configuration.Install;
using System.Diagnostics;
using System.Reflection;
using System.ServiceProcess;
using System.Threading;
using System.Threading.Tasks;
namespace ServiceMonitor
{
public class Program : ServiceBase
{
protected override void OnStart(string[] args)
{
runApplication();
}
private static void runApplication(bool console = false)
{
List<Timer> timerList = new List<Timer>(); //We store these in a list so GC does not eat them
ConfigurationModel configuration = new ConfigurationModel {
ServiceList = AppConfiguration.GetServiceListConfiguration(),
SMTPConfig = AppConfiguration.GetSMTPConfiguration() };
foreach (Service currentService in configuration.ServiceList)
{
timerList.Add(MonitorTimer.CreateTimer(currentService, configuration.SMTPConfig));
Task callTask = Task.Run(() => MonitorService.Monitor(currentService, configuration.SMTPConfig));
}
if (console)
{
while (true)
{
Console.WriteLine("Running!");
Console.ReadLine();
}
}
}
static void Main(string[] args)
{
if (Environment.UserInteractive && args.Length > 0)
{
try
{
switch (args[0])
{
case "-install":
{
Messages.LogMessage("Installing Service...");
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
break;
}
case "-uninstall":
{
Messages.LogMessage("Uninstalling Service...");
ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
break;
}
default:
runApplication(true);
break;
}
}
catch (Exception ex)
{
Messages.LogMessage(ex.Message, EventLogEntryType.Information, EventID.ServiceInstallationFailed);
}
}
else if (Environment.UserInteractive && args.Length == 0)
{
runApplication(true);
}
else
{
Run(new Program());
}
}
}
}