1 |
using System;
|
2 |
using System.Threading;
|
3 |
|
4 |
using System.Diagnostics;
|
5 |
|
6 |
using System.ServiceModel;
|
7 |
using System.ServiceModel.Description;
|
8 |
|
9 |
|
10 |
namespace MQFilter
|
11 |
{
|
12 |
|
13 |
[System.ComponentModel.DesignerCategory("Code")]
|
14 |
class PumpService : System.ServiceProcess.ServiceBase
|
15 |
{
|
16 |
public const int SECONDS_BETWEEN_RUN = 10;
|
17 |
const int INTERVAL = 5;
|
18 |
TimeSpan m_delay = new TimeSpan(0, 0, 0, INTERVAL, 0);
|
19 |
|
20 |
private Thread m_thread = null;
|
21 |
private static ManualResetEvent m_shutdownEvent = new ManualResetEvent(false);
|
22 |
|
23 |
FilterController filterController = null;
|
24 |
|
25 |
|
26 |
protected void InitializeComponent()
|
27 |
{
|
28 |
|
29 |
// Initialize the operating properties for the service.
|
30 |
this.CanPauseAndContinue = false;
|
31 |
this.CanShutdown = true;
|
32 |
this.CanHandleSessionChangeEvent = true;
|
33 |
this.ServiceName = "DaoMqPump2";
|
34 |
}
|
35 |
|
36 |
// Start the service.
|
37 |
protected override void OnStart(string[] args)
|
38 |
{
|
39 |
try
|
40 |
{
|
41 |
//first load transports
|
42 |
filterController = FilterController.getInstance();
|
43 |
|
44 |
|
45 |
//finally start the worker thread
|
46 |
// create our threadstart object to wrap our delegate method
|
47 |
ThreadStart ts = new ThreadStart(this.ServiceWorkerMethod);
|
48 |
|
49 |
// create the manual reset event and
|
50 |
// set it to an initial state of unsignaled
|
51 |
m_shutdownEvent = new ManualResetEvent(false);
|
52 |
|
53 |
// create the worker thread
|
54 |
m_thread = new Thread(ts);
|
55 |
|
56 |
// go ahead and start the worker thread
|
57 |
m_thread.Start();
|
58 |
|
59 |
}
|
60 |
catch (Exception e)
|
61 |
{
|
62 |
EventLog.WriteEntry("MQFilter", "Error starting DaoMqPump2: " + e.Message, EventLogEntryType.Error);
|
63 |
throw e;
|
64 |
}
|
65 |
}
|
66 |
|
67 |
// Stop this service.
|
68 |
protected override void OnStop()
|
69 |
{
|
70 |
// New in .NET Framework version 2.0.
|
71 |
this.RequestAdditionalTime(10000);
|
72 |
|
73 |
// signal the event to shutdown
|
74 |
m_shutdownEvent.Set();
|
75 |
|
76 |
// wait for the thread to stop giving it 10 seconds
|
77 |
m_thread.Join(10000);
|
78 |
|
79 |
// call the base class
|
80 |
base.OnStop();
|
81 |
|
82 |
this.ExitCode = 0;
|
83 |
}
|
84 |
|
85 |
|
86 |
public void ServiceWorkerMethod()
|
87 |
{
|
88 |
bool bSignaled = false;
|
89 |
int count = 20; //count starts high so we get to pump during the first iteration
|
90 |
try
|
91 |
{
|
92 |
do
|
93 |
{
|
94 |
// Block if the service is paused or is shutting down.
|
95 |
bSignaled = m_shutdownEvent.WaitOne(m_delay, true);
|
96 |
|
97 |
// if we were signaled to shutdow, exit the loop
|
98 |
if (bSignaled == true)
|
99 |
break;
|
100 |
|
101 |
count ++;
|
102 |
|
103 |
int elapsed = INTERVAL * count;
|
104 |
if (elapsed >= SECONDS_BETWEEN_RUN) // only run every SECONDS_BETWEEN_RUN'th second
|
105 |
{
|
106 |
filterController.transportAllMessages();
|
107 |
count = 0;//reset counter
|
108 |
}
|
109 |
|
110 |
}
|
111 |
while (true);
|
112 |
}
|
113 |
catch (ThreadAbortException)
|
114 |
{
|
115 |
// Another thread has signalled that this worker
|
116 |
// thread must terminate. Typically, this occurs when
|
117 |
// the main service thread receives a service stop
|
118 |
// command.
|
119 |
|
120 |
// Write a trace line indicating that the worker thread
|
121 |
// is exiting. Notice that this simple thread does
|
122 |
// not have any local objects or data to clean up.
|
123 |
}
|
124 |
}
|
125 |
|
126 |
|
127 |
}
|
128 |
}
|