-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
52 lines (44 loc) · 1.69 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
using Spooler.Properties;
using System.Diagnostics;
using System.ServiceProcess;
ServiceController spoolService = new ServiceController("spooler");
void EventLogWrite(string message, EventLogEntryType type, int codeEventId)
{
string sourceForLog = "SpoolerRestarter";
EventLog.WriteEntry(sourceForLog, message, type, codeEventId);
}
try
{
switch (spoolService.Status)
{
case ServiceControllerStatus.Running:
spoolService.Stop();
spoolService.WaitForStatus(ServiceControllerStatus.Stopped);
spoolService.Start();
spoolService.WaitForStatus(ServiceControllerStatus.Running);
EventLogWrite(Resources.Restarted, EventLogEntryType.Warning, 150);
break;
case ServiceControllerStatus.Stopped:
spoolService.Start();
spoolService.WaitForStatus(ServiceControllerStatus.Running);
EventLogWrite(Resources.Started, EventLogEntryType.Warning, 150);
break;
case ServiceControllerStatus.Paused:
spoolService.Start();
spoolService.WaitForStatus(ServiceControllerStatus.Running);
EventLogWrite(Resources.Started, EventLogEntryType.Warning, 150);
break;
default:
spoolService.Stop();
spoolService.WaitForStatus(ServiceControllerStatus.Stopped);
spoolService.Start();
spoolService.WaitForStatus(ServiceControllerStatus.Running);
EventLogWrite(Resources.UnknownRestarted, EventLogEntryType.Warning, 150);
break;
}
}
catch (InvalidOperationException)
{
EventLogWrite(Resources.UnknownError, EventLogEntryType.Error, 160);
}
spoolService.Close();