-
Notifications
You must be signed in to change notification settings - Fork 0
/
Service.cs
96 lines (88 loc) · 2.43 KB
/
Service.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
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Configuration.Install;
using System.Reflection;
using System.Windows.Forms;
namespace SiteMonitorService
{
public class SiteMonitor : System.ServiceProcess.ServiceBase
{
private System.ComponentModel.Container components = null;
private SiteMonitorService.Engine _engine;
public SiteMonitor()
{
InitializeComponent();
}
static void Main(string[] args)
{
string opt=null;
if(args.Length >0 )
{
opt=args[0];
}
if(opt!=null && opt.ToLower()=="/install")
{
TransactedInstaller ti= new TransactedInstaller();
MyInstaller pi = new MyInstaller();
ti.Installers.Add(pi);
String path=String.Format("/assemblypath={0}",
System.Reflection.Assembly.GetExecutingAssembly().Location);
String[] cmdline={path};
InstallContext ctx = new InstallContext("",cmdline);
ti.Context =ctx;
ti.Install(new Hashtable());
}
else if (opt !=null && opt.ToLower()=="/uninstall")
{
TransactedInstaller ti=new TransactedInstaller();
MyInstaller mi=new MyInstaller();
ti.Installers.Add(mi);
String path = String.Format("/assemblypath={0}",
System.Reflection.Assembly.GetExecutingAssembly().Location);
String[] cmdline={path};
InstallContext ctx = new InstallContext("",cmdline);
ti.Context=ctx;
ti.Uninstall(null);
}
if(opt==null) // e.g. ,nothing on the command line
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new SiteMonitor() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
}
private void InitializeComponent()
{
this._engine = new Engine();
this.CanHandlePowerEvent = true;
this.CanPauseAndContinue = true;
this.CanShutdown = true;
this.ServiceName = "SiteMonitor";
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
protected override void OnStart(string[] args)
{
// Debugger.Launch(); // use this to help debug - attach to a running service
_engine.BeginMonitor();
EventLog.WriteEntry("SiteMonitorService starting. " );
}
protected override void OnStop()
{
_engine.EndMonitor();
}
}
}