Creating a Windows Services in C#

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Diagnostics;  
  6. using System.IO;  
  7. using System.Linq;  
  8. using System.ServiceProcess;  
  9. using System.Text;  
  10. using System.Threading;  
  11. using System.Threading.Tasks;  
  12. using System.Timers;  
  13. namespace MyWinService   
  14. {  
  15.     public partial class Service1: ServiceBase   
  16.     {  
  17.         public Service1()   
  18.         {  
  19.             InitializeComponent();  
  20.         }  
  21.         //************************************************************************************************************  
  22.         //Initialize the timer  
  23.         System.Timers.Timer timer = new System.Timers.Timer();  
  24.         //************************************************************************************************************  
  25.         //This method is used to raise event during start of service  
  26.         protected override void OnStart(string[] args)   
  27.         {  
  28.             //add this line to text file during start of service  
  29.             TraceService("Start service");  
  30.             //handle Elapsed event  
  31.             timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);  
  32.             //This statement is used to set interval to 30 Seconds (= 30,000 milliseconds)  
  33.             timer.Interval = 30000;  
  34.             //enabling the timer  
  35.             timer.Enabled = true;  
  36.         }  
  37.         //************************************************************************************************************  
  38.         //This method is used to stop the service  
  39.         protected override void OnStop()   
  40.         {  
  41.             timer.Enabled = false;  
  42.             TraceService("Stopping service");  
  43.         }  
  44.         //************************************************************************************************************  
  45.         private void OnElapsedTime(object source, ElapsedEventArgs e)   
  46.         {  
  47.             TraceService("Another entry at " + DateTime.Now);  
  48.         }  
  49.         //************************************************************************************************************  
  50.         private void TraceService(string content)   
  51.         {  
  52.             //set up a filestream  
  53.             FileStream fs = new FileStream(@  
  54.             "d:\Service1.txt", FileMode.OpenOrCreate, FileAccess.Write);  
  55.             //set up a streamwriter for adding text  
  56.             StreamWriter sw = new StreamWriter(fs);  
  57.             //find the end of the underlying filestream  
  58.             sw.BaseStream.Seek(0, SeekOrigin.End);  
  59.             //add the text  
  60.             sw.WriteLine(content);  
  61.             //add the text to the underlying filestream  
  62.             sw.Flush();  
  63.             //close the writer  
  64.             sw.Close();  
  65.         }  
  66.         //************************************************************************************************************  
  67.     }  
  68. }