-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampleService.cs
More file actions
102 lines (93 loc) · 1.87 KB
/
SampleService.cs
File metadata and controls
102 lines (93 loc) · 1.87 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using System.Threading;
using System.Diagnostics;
namespace TestBench
{
public class SampleService : ServiceBase
{
private Thread m_worker;
private bool m_cancel;
private bool m_paused;
private EventLog m_eventLog;
public static readonly string EVENTLOGNAME = "SampleService";
public static readonly string EVENTLOGSOURCE = "SampleService";
public SampleService()
{
this.CanShutdown = true;
this.CanStop = true;
this.CanPauseAndContinue = true;
this.CanHandlePowerEvent = false;
this.CanHandleSessionChangeEvent = false;
this.m_cancel = false;
}
public void Start()
{
m_cancel = false;
if(m_worker != null && m_worker.IsAlive)
{
return;
}
m_worker = new Thread(new ThreadStart(DoWork));
m_worker.Start();
}
public new void Stop()
{
m_cancel = true;
base.Stop();
}
protected override void OnStart(string[] args)
{
this.Start();
base.OnStart(args);
}
protected override void OnContinue()
{
this.m_paused = false;
base.OnContinue();
}
protected override void OnPause()
{
this.m_paused = true;
base.OnPause();
}
private void DoWork()
{
// While/true are evil but in a service loops, its allowed
while(true)
{
if(m_cancel)
{
break;
}
if(m_paused)
{
// Sleep and then skip doing actual work
Sleep(1000);
continue;
}
// Actual Work Stuff
// Give the other things a changes to do things
Sleep(1000);
}
}
private void Sleep(int timeOut)
{
Thread.Sleep(timeOut);
}
public override System.Diagnostics.EventLog EventLog
{
get
{
if (m_eventLog == null)
{
m_eventLog = new EventLog(EVENTLOGNAME, Environment.MachineName, EVENTLOGSOURCE);
}
return m_eventLog;
}
}
}
}