using System; using System.Threading; using System.Diagnostics; using System.ServiceModel; using System.ServiceModel.Description; namespace MQFilter { [System.ComponentModel.DesignerCategory("Code")] class PumpService : System.ServiceProcess.ServiceBase { const int INTERVAL = 5; TimeSpan m_delay = new TimeSpan(0, 0, 0, INTERVAL, 0); private Thread m_thread = null; private static ManualResetEvent m_shutdownEvent = new ManualResetEvent(false); FilterController filterController = null; protected void InitializeComponent() { // Initialize the operating properties for the service. this.CanPauseAndContinue = false; this.CanShutdown = true; this.CanHandleSessionChangeEvent = true; this.ServiceName = "DaoMqPump2"; } // Start the service. protected override void OnStart(string[] args) { try { //first load transports filterController = FilterController.getInstance(); //finally start the worker thread // create our threadstart object to wrap our delegate method ThreadStart ts = new ThreadStart(this.ServiceWorkerMethod); // create the manual reset event and // set it to an initial state of unsignaled m_shutdownEvent = new ManualResetEvent(false); // create the worker thread m_thread = new Thread(ts); // go ahead and start the worker thread m_thread.Start(); } catch (Exception e) { EventLog.WriteEntry("MQFilter", "Error starting DaoMqPump2: " + e.Message, EventLogEntryType.Error); throw e; } } // Stop this service. protected override void OnStop() { // New in .NET Framework version 2.0. this.RequestAdditionalTime(10000); // signal the event to shutdown m_shutdownEvent.Set(); // wait for the thread to stop giving it 10 seconds m_thread.Join(10000); // call the base class base.OnStop(); this.ExitCode = 0; } public void ServiceWorkerMethod() { bool bSignaled = false; int count = 20; //count starts high so we get to pump during the first iteration try { do { // Block if the service is paused or is shutting down. bSignaled = m_shutdownEvent.WaitOne(m_delay, true); // if we were signaled to shutdow, exit the loop if (bSignaled == true) break; count ++; int elapsed = INTERVAL * count; if (elapsed >= 30) // only run every 30th second { filterController.transportAllMessages(); count = 0;//reset counter } } while (true); } catch (ThreadAbortException) { // Another thread has signalled that this worker // thread must terminate. Typically, this occurs when // the main service thread receives a service stop // command. // Write a trace line indicating that the worker thread // is exiting. Notice that this simple thread does // not have any local objects or data to clean up. } } } }