-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathHabSampleService.cs
64 lines (57 loc) · 1.9 KB
/
HabSampleService.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
using System.Configuration;
using System.IO;
using System.Reflection;
using System.ServiceProcess;
using System;
namespace HabitatSample
{
public partial class HabSampleService : ServiceBase
{
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new HabSampleService()
};
ServiceBase.Run(ServicesToRun);
}
public HabSampleService()
{
ServiceName = "HabSampleService";
CanStop = true;
AutoLog = true;
}
protected override void OnStart(string[] args)
{
WriteStatus("HabSampleService is starting");
System.Timers.Timer timer = new System.Timers.Timer
{
Interval = 60000 // 60 seconds
};
timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimer);
timer.Start();
WriteStatus("HabSampleService is started");
}
protected override void OnStop()
{
WriteStatus("HabSampleService is stopped");
}
public void OnTimer(object sender, System.Timers.ElapsedEventArgs args)
{
WriteStatus("HabSampleService is up");
}
private static void WriteStatus(string message)
{
var statusFileDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (ConfigurationManager.AppSettings["statusFileDir"] != null)
{
statusFileDir = ConfigurationManager.AppSettings["statusFileDir"];
}
using (var tw = new StreamWriter(Path.Combine(statusFileDir, "status.txt"), true))
{
tw.WriteLine(DateTime.Now.ToString() + ": " + message);
}
}
}
}