views:

270

answers:

5

I know how to "create" a Service application.

I know what to write in the main() function.

I know how to add an EventLog (or other components) to the service.

I know how to define what happens when the service starts, stops or resumes.

What I want to know is this...

I want my Windows Service to perform certain function (like Indexing files).

Where do I add the code to perform this function in the Service application?

A: 

My guess is that you'd want to index every X minutes, so you'd probably want to set a Timer in the Start event for the service, then perform your indexing when that timer fires.

Will
+1  A: 

I am going to assume that by "indexer function" you are referring to a library function you have written that you wish to run by means of a windows service. If this is the case then add your code to this overriden method:

protected override void OnStart(String[] args)
{
    // your stuff here
}

This code will fire when the service starts.

Andrew Hare
That's it...? Just add my block of code to OnStart without worrying about how long does it take to execute my code?
Yes, it will run as long as it needs too. Remember that this can be as dangerous as it is convenient.
Andrew Hare
The OnStart method should never perform any work other than some quick set up and then launching the real work asynchronously. Either via a timer, a new thread or as a work item for the thread pool. If the OnStart method does not return quickly the service will be considered to be in an error state.
Stephen Martin
Good point, Stephen - thanks!
Andrew Hare
A: 

I would say that depends on when your Indexer function should run. If it should start running as soon as the Service starts, you would call the function in the OnStart method. If it is to be started as a result of a Custom Event, then you would call it in the Handler for that Event. If the function needs to be called after specific intervals of time, you might want to use a Timer.

Cerebrus
+1  A: 

I'm assuming that you want something that will run periodically, not constantly. You might want to consider setting up a timer that executes a method on expiration. The timer could reset itself automatically or your callback could reset the timer for its next expiration when it completes. You'd initialize the timer in your OnStart method. You need to be careful to stop the timer when the service is paused or stopped, and clean things up on shutdown.

You may also want to consider lowering the priority of your service so that it doesn't pre-empt foreground tasks on the system.

tvanfosson
A: 

Simple example of repeating action service, based on System.Timers.Timer:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Timers;

namespace SrvControl
{
public partial class Service1 : ServiceBase
{
    Timer mytimer;
    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        if (mytimer == null)
            mytimer = new Timer(5 * 1000.0);
        mytimer.Elapsed += new ElapsedEventHandler(mytimer_Elapsed);
        mytimer.Start();
    }

    void mytimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        //Do Anything, e.g. write to eventlog
    }

    protected override void OnStop()
    {
        mytimer.Stop();
    }
}
}
gimel
Timers in services are bad, no downvote, but I would not recommend them. http://stackoverflow.com/questions/397744/-net-windows-service-with-timer-stops-responding
StingyJack
A recent note in Q397744 says: both services are running without any problem since then. Please mark it as "Closed for not relevant anymore".
gimel