PageRenderTime 55ms CodeModel.GetById 46ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

/Visual Studio 2008/CSWindowsService/SampleService.cs

#
C# | 116 lines | 40 code | 16 blank | 60 comment | 1 complexity | 4b34c05fb6b60c72173a41a9a6b0d60a MD5 | raw file
  1/****************************** Module Header ******************************\
  2* Module Name:  SampleService.cs
  3* Project:      CSWindowsService
  4* Copyright (c) Microsoft Corporation.
  5* 
  6* Provides a sample service class that derives from the service base class - 
  7* System.ServiceProcess.ServiceBase. The sample service logs the service 
  8* start and stop information to the Application event log, and shows how to 
  9* run the main function of the service in a thread pool worker thread. 
 10* 
 11* This source is subject to the Microsoft Public License.
 12* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
 13* All other rights reserved.
 14* 
 15* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 16* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 17* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
 18\***************************************************************************/
 19
 20#region Using directives
 21using System;
 22using System.ComponentModel;
 23using System.Diagnostics;
 24using System.ServiceProcess;
 25using System.Threading;
 26#endregion
 27
 28
 29namespace CSWindowsService
 30{
 31    public partial class SampleService : ServiceBase
 32    {
 33        public SampleService()
 34        {
 35            InitializeComponent();
 36
 37            this.stopping = false;
 38            this.stoppedEvent = new ManualResetEvent(false);
 39        }
 40
 41
 42        /// <summary>
 43        /// The function is executed when a Start command is sent to the 
 44        /// service by the SCM or when the operating system starts (for a 
 45        /// service that starts automatically). It specifies actions to take 
 46        /// when the service starts. In this code sample, OnStart logs a 
 47        /// service-start message to the Application log, and queues the main 
 48        /// service function for execution in a thread pool worker thread.
 49        /// </summary>
 50        /// <param name="args">Command line arguments</param>
 51        /// <remarks>
 52        /// A service application is designed to be long running. Therefore, 
 53        /// it usually polls or monitors something in the system. The 
 54        /// monitoring is set up in the OnStart method. However, OnStart does 
 55        /// not actually do the monitoring. The OnStart method must return to 
 56        /// the operating system after the service's operation has begun. It 
 57        /// must not loop forever or block. To set up a simple monitoring 
 58        /// mechanism, one general solution is to create a timer in OnStart. 
 59        /// The timer would then raise events in your code periodically, at 
 60        /// which time your service could do its monitoring. The other 
 61        /// solution is to spawn a new thread to perform the main service 
 62        /// functions, which is demonstrated in this code sample.
 63        /// </remarks>
 64        protected override void OnStart(string[] args)
 65        {
 66            // Log a service start message to the Application log.
 67            this.eventLog1.WriteEntry("CSWindowsService in OnStart.");
 68
 69            // Queue the main service function for execution in a worker thread.
 70            ThreadPool.QueueUserWorkItem(new WaitCallback(ServiceWorkerThread));
 71        }
 72
 73
 74        /// <summary>
 75        /// The method performs the main function of the service. It runs on 
 76        /// a thread pool worker thread.
 77        /// </summary>
 78        /// <param name="state"></param>
 79        private void ServiceWorkerThread(object state)
 80        {
 81            // Periodically check if the service is stopping.
 82            while (!this.stopping)
 83            {
 84                // Perform main service function here...
 85
 86                Thread.Sleep(2000);  // Simulate some lengthy operations.
 87            }
 88
 89            // Signal the stopped event.
 90            this.stoppedEvent.Set();
 91        }
 92
 93
 94        /// <summary>
 95        /// The function is executed when a Stop command is sent to the 
 96        /// service by SCM. It specifies actions to take when a service stops 
 97        /// running. In this code sample, OnStop logs a service-stop message 
 98        /// to the Application log, and waits for the finish of the main 
 99        /// service function.
100        /// </summary>
101        protected override void OnStop()
102        {
103            // Log a service stop message to the Application log.
104            this.eventLog1.WriteEntry("CSWindowsService in OnStop.");
105
106            // Indicate that the service is stopping and wait for the finish 
107            // of the main service function (ServiceWorkerThread).
108            this.stopping = true;
109            this.stoppedEvent.WaitOne();
110        }
111
112
113        private bool stopping;
114        private ManualResetEvent stoppedEvent;
115    }
116}