As mentioned, I am starting off the technical posts.
I found the following use of Windows services very useful. Just wanted to share this with everyone.
There are certain situations where you need to monitor changes, and trigger actions, like changes in data in a database. Sure you can use database triggers, but here is an alternative to using database triggers, which involves using a Windows service.
I will not go over the basic steps of creating a Windows service here. Rather, I will go through the idea of polling for information in a Windows service. If interested in the basic structure of a Windows service, this article in CodeProject is a good source of information.
A service class is created. Apologies for the (lack of) indentation - I blame the post editor:-
1: public class MonitorService : System.ServiceProcess.ServiceBase
2: {
3: 4: 5: private System.ComponentModel.Container components = null;
6: ServicePollingService pollingService;
7:
8:
9: public MonitorService()
10: {
11:
12: InitializeComponent();
13: }
14:
15:
16:
17: static void Main()
18: {
19: System.ServiceProcess.ServiceBase[] ServicesToRun;
20:
21:
22:
23:
24:
25:
26: ServicesToRun =
new System.ServiceProcess.ServiceBase[] { new PollingService() };
27: System.ServiceProcess.ServiceBase.Run(ServicesToRun);
28: }29: 30:
31: private void InitializeComponent()
32: {
33: components = new System.ComponentModel.Container();
34: this.ServiceName = "Monitor Service";
35: }36: 37: protected override void Dispose( bool disposing )
38: {
39: if( disposing )
40: {
41: if (components != null)
42: {
43: components.Dispose();
44: }
45: }
46: base.Dispose( disposing );
47: }48: 49: protected override void OnStart(string[] args)
50: {
51: pollingService = new PollingService();
52: pollingService.Start();
53: }54: 55: protected override void OnPause()
56: {
57: pollingService.Suspend();
58: }59: 60: protected override void OnContinue()
61: {
62: pollingService.Resume();
63: }64: 65: protected override void OnStop()
66: {
67: pollingService.Stop();
68: }
69: }
Like most service classes, PollingService
has a Start
and a Stop
method. The Start
method starts a listener thread which monitors changes - it could periodically run another function/Stored Procedure, etc., to "poll" or look for changes. This is shown below:
1: public class PollingService
2: {
3: private Thread listenerThread;
4:
5:
6:
7:
8:
9: private const int RUN = 2;
10: private const int STOP = 3;
11:
12:
13: private int state = STOPPED;
14: public const string ServiceName = "Polling Service";
15:
16: public PollingService()
17: {
18: } 19: 20: public void Start()
21: {
22: listenerThread = new Thread(new ThreadStart(this.Listener));
23: listenerThread.Start();
24: }
25:
26: 27: 28: public void Stop()
29: {
30:
31:
32:
33: state = STOP;
34: listenerThread.Interrupt();
35:
36: }
37:
38: 39: 40:
41: 42: public void Suspend()
43: {
44: listenerThread.Suspend();
45:
46: }
47: 48: 49: 50:
51: public void Resume()
52: {
53:
54: listenerThread.Resume();
55: }
56:
57: 58: 59: 60:
61: protected void Listener()
62: {
63: try
64: {
65: state = RUN;
66: while ( state == RUN )
67: {
68: PollForChanges();
69:
70:
71:
72: Thread.Sleep(new TimeSpan(hoursInterval, minutesInterval, secondsInterval));
73: }
74: }
75: catch ( ThreadInterruptedException )
76: {
77:
78:
79: catch ( Exception e)
80: {
81:
82:
83:
84: LogHelper.LogError(e.ToString());
85: }
86: finally
87: {
88: state = STOPPED;
89: LogHelper.LogWarning(string.Format("The {0} has shutdown.", ServiceName));
90: }
91: }
92:
93: 94: 95: 96: 97:
98: private void PollForChanges()
99: {
100: try
101: {
102:
103: ProcessChanges();
104: }
105: catch(Exception e)
106: {
107: LogHelper.LogError(e.ToString());
108: }
109: finally
110: {
111:
112: }
113: }
These types of services have proved extremely useful for us over the past few years - it has allowed us to monitor changes, pull/push data - quite good for integrating diverse systems.
Hope you find this useful.