ARTICLE

Creating a Windows Service in C#

Posted by pravin jayakumar Articles | Windows Services in C# June 12, 2008
This article shows how to create a simple Windows Service using C# and Visual Studio 2005.
Reader Level:
Download Files:
 

In this step-by-step article, I talk about how to create a Windows Service using Visual Studio 2005 and .NET Framework 2.0. 

In Visual Studio 2005,
 
Step 1:  Select File -> New -> Project

Step 2:  Add new project dialog box opens. In that left side pane, expand Visual C#. Click on Windows. In the right side pane, some templates will be displayed.

Select Windows Service from the installed templates. Give some name for the windows service.

 

Step 3:  Once you click "OK" in the above dialog box, the following screen appears.

  

Step 4:  Now click on the link ("Click here to switch to code view") and the following screen appears

Step 5:  Now replace the overridden OnStart and OnStop methods will the following code. The OnStart event code executes when a service starts and OnStop event code executes when the service stops.

On the OnStart event, I create a text file and have a timer settings. On the OnStop event, I write more text to the text file.

        protected override void OnStart(string[] args)

        {

            FileStream fs = new FileStream(@"c:\temp\pravin.txt",

            FileMode.OpenOrCreate, FileAccess.Write);

            StreamWriter m_streamWriter = new StreamWriter(fs);

            m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);

           

            m_streamWriter.WriteLine("Service Started on \n" + DateTime.Now.ToShortDateString() + " at " + DateTime.Now.ToShortTimeString());

            m_streamWriter.Flush();

            m_streamWriter.Close();

            _timer.Change(0, 30000); // Commented Lines

           

        }

        protected override void OnStop()

        {

            FileStream fs = new FileStream(@"c:\temp\mcWindowsService.txt",

            FileMode.OpenOrCreate, FileAccess.Write);

            StreamWriter m_streamWriter = new StreamWriter(fs);

            m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);

            m_streamWriter.WriteLine(" mcWindowsService: Service Stopped \n" + DateTime.Now.ToShortDateString() + " at " + DateTime.Now.ToShortTimeString());

            m_streamWriter.WriteLine(" *-------------* \n");

            m_streamWriter.Flush();

            m_streamWriter.Close();

        }

Step 6:  Refer Step 4.  The constructor service  can be replaced with the following code, if the windows service is to be triggered at regular interval

Timer _timer;

public Service1()

{

     _timer = new Timer(new TimerCallback(OnNextMinute), null, Timeout.Infinite, Timeout.Infinite);            

     InitializeComponent();

}

 

 

 

-----------------------------------------------------------------------------------
EDITOR'S NOTE:
Please refer
Creating a Windows Service in C#  article to learn how to install, start, and stop a Windows Service.

------------------------------------------------------------------------------------

Login to add your contents and source code to this article
post comment
     

how can i connect to a db from the windows server?
whats the best approach?
im very new to C#

Posted by tina Oct 26, 2010

Nice Article

Posted by Amit k Sep 16, 2009

hi;
the article is good ..but i have some doubts and errors coming which iam unable to solve....what should i do..

Posted by Mumtaz Surani Jul 16, 2009

i think you can write your own method in replace of that method, this is just an example that method will change in 30000 mili seconds.

Posted by Raj Kumar May 03, 2009

Thank you for the great example! Do you have code for the OnNextMinute call back method?

Posted by Jay Jan 06, 2009
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter