PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms 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. #region Using directives
  20. using System;
  21. using System.ComponentModel;
  22. using System.Diagnostics;
  23. using System.ServiceProcess;
  24. using System.Threading;
  25. #endregion
  26. namespace CSWindowsService
  27. {
  28. public partial class SampleService : ServiceBase
  29. {
  30. public SampleService()
  31. {
  32. InitializeComponent();
  33. this.stopping = false;
  34. this.stoppedEvent = new ManualResetEvent(false);
  35. }
  36. /// <summary>
  37. /// The function is executed when a Start command is sent to the
  38. /// service by the SCM or when the operating system starts (for a
  39. /// service that starts automatically). It specifies actions to take
  40. /// when the service starts. In this code sample, OnStart logs a
  41. /// service-start message to the Application log, and queues the main
  42. /// service function for execution in a thread pool worker thread.
  43. /// </summary>
  44. /// <param name="args">Command line arguments</param>
  45. /// <remarks>
  46. /// A service application is designed to be long running. Therefore,
  47. /// it usually polls or monitors something in the system. The
  48. /// monitoring is set up in the OnStart method. However, OnStart does
  49. /// not actually do the monitoring. The OnStart method must return to
  50. /// the operating system after the service's operation has begun. It
  51. /// must not loop forever or block. To set up a simple monitoring
  52. /// mechanism, one general solution is to create a timer in OnStart.
  53. /// The timer would then raise events in your code periodically, at
  54. /// which time your service could do its monitoring. The other
  55. /// solution is to spawn a new thread to perform the main service
  56. /// functions, which is demonstrated in this code sample.
  57. /// </remarks>
  58. protected override void OnStart(string[] args)
  59. {
  60. // Log a service start message to the Application log.
  61. this.eventLog1.WriteEntry("CSWindowsService in OnStart.");
  62. // Queue the main service function for execution in a worker thread.
  63. ThreadPool.QueueUserWorkItem(new WaitCallback(ServiceWorkerThread));
  64. }
  65. /// <summary>
  66. /// The method performs the main function of the service. It runs on
  67. /// a thread pool worker thread.
  68. /// </summary>
  69. /// <param name="state"></param>
  70. private void ServiceWorkerThread(object state)
  71. {
  72. // Periodically check if the service is stopping.
  73. while (!this.stopping)
  74. {
  75. // Perform main service function here...
  76. Thread.Sleep(2000); // Simulate some lengthy operations.
  77. }
  78. // Signal the stopped event.
  79. this.stoppedEvent.Set();
  80. }
  81. /// <summary>
  82. /// The function is executed when a Stop command is sent to the
  83. /// service by SCM. It specifies actions to take when a service stops
  84. /// running. In this code sample, OnStop logs a service-stop message
  85. /// to the Application log, and waits for the finish of the main
  86. /// service function.
  87. /// </summary>
  88. protected override void OnStop()
  89. {
  90. // Log a service stop message to the Application log.
  91. this.eventLog1.WriteEntry("CSWindowsService in OnStop.");
  92. // Indicate that the service is stopping and wait for the finish
  93. // of the main service function (ServiceWorkerThread).
  94. this.stopping = true;
  95. this.stoppedEvent.WaitOne();
  96. }
  97. private bool stopping;
  98. private ManualResetEvent stoppedEvent;
  99. }
  100. }