views:

1672

answers:

2

I've a .net WCF windows service running on an app server which keeps monitoring a folder for xml files. How do I make this service run only at a particular time (01.00 hours) everyday ?

Thanks.

+3  A: 

Is your service a real service or just a WCF application which monitors the XML folder?

In case your WCF service is just a normal application the easiest way is to use the Scheduled Tasks functionality in Windows (Found in Control Panel). Just have the application check the folder on startup and set a scheduled task which starts the application at whatever time you want.

If the target application is a real Windows Service, you need to use an internal timer. Have a look at the System.Timers.Timer class.

public void OnLoad() {
    Timer timer = new Timer();

    // Add event handler
    timer.Elapsed += WorkMethod;

    // Give us more control over the timer.
    timer.AutoReset = false;

    SetupTimer(timer);
}

// Setups the timer for the next interval and starts it.
private void SetupTimer(Timer timer) {
    timer.Interval = GetNextChecktime().Subtract(DateTime.Now).TotalMillisecond;

    timer.Start();
}

private void WorkMethod(object sender, EventArgs e) {
    // Do work here

    // Setup the timer for the next cycle.
    SetupTimer((Timer)sender);
}

private DateTime GetNextChecktime() {
    // Return the next time the service should run as a datetime.
}

The reason for using SetupTimer instead of just using AutoReset = true which would repeat automatically is to synchronize the timer with the GetNextChecktime(). Just using 24*60*60*1000 milliseconds as the elapsed timer would give 24 hour phase but you'd need to start the script at 01:00 to have it run daily at 01:00.

If you can still affect the way the application is run, I'd actually recommend the first method. Unless you have more functionality in the service or need to maintain some persistent state it's simpler to just have an application which monitors the folder on startup and then exits. It's also easier to debug and less error prone as the scheduling is done by Windows.

Mikko Rantanen
what if my WCf service is a web service
Meetu Choudhary
A: 

If you need to run your service only once or twice a day (or even only once a week), I'd create a console app and schedule it to run at these chosen times using Windows "Scheduled Tasks". That way, only the Windows system itself will be checking whether or not the "right" time was already arrived to wake up your service and do the processing.

If you have a service that needs to run around the clock and e.g. only process messages during a given window of time, then a Windows (NT) Service might be your choice, which runs in the background, runs on a server without a user being logged on, and can do all sorts of things, like check the time to see whether or not it needs to work, and if not, it can keep on sleeping on :-)

From my experience, services are a bit trickier to program and to debug - if you can package your logic into a standalone console app and run it as scheduled tasks, I'd prefer that solution. It's easier and still does the trick nicely.

Marc

marc_s
Is it possible to schedule a console app in such a way that it does not display a window when executed? I doubt it matters in this case as there shouldn't be anyone seeing it 01:00 - but just out of curiosity. I think I've tried this at some point without much luck.
Mikko Rantanen
I would think from my own experience that unless you WRITE to the Console using Console.Write/WriteLine, there won't be any visible console at all. So yes, I guess, you should be able to have a completely "invisible" console app run at 1am in the morning :-)
marc_s