Creating windows Services using C#


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Threading;


namespace services
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
/* Creating a method */
static void run()
{
/* using endless loop */
while (true)
{
/* A new file will be created in D: drive name as loop-file.txt
This file will keepon running till the service is not stopped*/

StreamWriter str = new StreamWriter("D:\\loop-file.txt", true);
str.WriteLine("This line will keepon printing in the file till the Services is not stoped.....");
str.Close();
}
}

/* Calling the method */
Thread thr = new Thread(new ThreadStart(run));

/* Below code will note the time on which service is started */
protected override void OnStart(string[] args)
{

/* A new file will be created in D: drive name as tim-file.txt
This file will note the time on which service had started & stopped*/
StreamWriter str = new StreamWriter("D:\\tim-file.txt",true);
str.WriteLine("Service started on : " + DateTime.Now.ToString());
str.Close();
thr.Start();
}

/* Below code will note the time on which service is stopped */
protected override void OnStop()
{
StreamWriter str = new StreamWriter("D:\\tim-file.txt", true);
str.WriteLine("Service stoped on : " + DateTime.Now.ToString());
str.Close();
thr.Abort();
}

}
}





I had also created the Windows Srevices MSWORD file attached, this file will help you while doing this project....

plz reffer it.


Attachments

  • How to create our own Service (28151-3453-How to create our own Service.doc)
  • How to create our own Service (28151-41024-How to create our own Service.doc)
  • Comments

    Author: Shailesh Pisat25 May 2009 Member Level: Gold   Points : 1

    Good use of Windows Services. Can this be used in any way in the Project.

    Thanks
    Shailesh

    Author: khaja08 Jun 2009 Member Level: Bronze   Points : 1

    Hey Chirag Oza You have given very good information . It helps alot for people who are new to windows services .. Very Good knowledge sharing in an effective way .
    Hope u help me if i get any issues regarding Windows services.

    Author: Abhinav Dawra08 Jun 2009 Member Level: Gold   Points : 0

    Excellent examples of web services in action.
    Very helpful for any newbie.

    Thanks,
    Abhinav



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: