/[projects]/dao/DaoMqPump2/MQFilter/FilterService.cs
ViewVC logotype

Annotation of /dao/DaoMqPump2/MQFilter/FilterService.cs

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20