Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Windows Service

Status
Not open for further replies.

EzehM

Programmer
Feb 27, 2003
86
GB
I am writing a piece of code that monitors files in a directory and copies them onto another directory, by calling other methods etc. I have a setting in the registry that determines the polling intervals. This part of the code is in an endless loop. I have written this as a clean .exe, but now I want to make it a Windows service. I have just started reading on Windows services, and I noticed that the OnStart() function where I had planned to place my code requires that it returns almost immediately (30 seconds). My question therefore is this: How can I then write this bit of code as a service. What do I need to place in the OnStart() function, and still make my application work? Especially since I cant loop indefinitely in the OnStart() function.

Many thanks in advance
 
Thanks for the tip, I have explored the FileSystemWatcher object, but because of the involvedness in our design and the use of various configuration elements to determine what directories to monitor, remove, add etc. The idea of using it was discarded. I have something that works, but all I need to know is what to add to the OnStart() method. I have just read a little bit about using the Timer object, and in some cases spawning a separate thread to call the function that polls while returning quickly to OnStart().
 
i created a service that runs once a day. turns on, run code, turn off. if I do this
Code:
Start()
{
   new MyTask().Preform();
   this.Stop();
}
then the service will work, but an error is logged in the event log stating the service may not have started, or an error occurred. instead I need to do this
Code:
Start()
{
   this.timer.Start();
}

Timer_Elapsed()
{
   this.timer.Stop();
   new MyTask().Preform();
   this.Stop();
}
now the order of events is
1. start service
2. service starts timer
3. service completes starting
4. timer event executes my task
5. service is stopped
everything runs as before, and no errors logged to the event log. full code sample:
Code:
public class MyService : BaseService
{
   private Timer timer;

   public MyService()
   {
      this.timer = new Timer();
      this.timer.Stop();
      this.timer.Elapsed += new TimerHandler(timer_Elapsed);
      this.timer.Ticks = 2000; //2 seconds
   }

   protected override void OnStart()
   {
      this.timer.Start();
   }

   protected override void OnStop()
   {
      this.timer.Stop();
   }

   private void timer_Elapsed(object sender, TimerEventArgs e)
   {
      this.timer.Stop();
      new MyTask().Preform();
      this.Stop();
   }
}
if you don't want to automatically stop the service change the timer event to this
Code:
   private void timer_Elapsed(object sender, TimerEventArgs e)
   {
      this.timer.Stop();
      new MyTask().Preform();
      this.timer.Start();
   }

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks Jason, just what I needed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top