developer
developer

Reputation: 1675

Run .Net method once in a month

I have a windows service using .Net framework which needs to do some "work" once in a month. What is the best way to implement this?

I can think of the following options:

1) Do the "work" and sleep for 2592000 seconds (1 month equivalent).

2) Do the "work" and continue looping until desired date/time has reached? (sleeping for about 1 minute inside the loop).

3) Change it to a console application and let windows scheduler run the console application at specified date/time.

4) Any other way to force .net to start / continue the thread at specific date/time?

Upvotes: 1

Views: 2443

Answers (3)

Craig H
Craig H

Reputation: 2071

You can create a System.Threading.Timer object, which performs a callback at a specified interval.
I have used this for a windows service previously, and in the service I have a timer property like so:

public partial class SomeService : ServiceBase
{
    private Timer scheduler = null;

    private Timer Scheduler
    {
        get
        {
            if (this.scheduler == null)
            {
                this.scheduler = new Timer(new TimerCallback(this.ProcessingFunction));
            }
            return this.scheduler;
        }
    }

    protected override void OnStart(string[] args)
    {
        // Set first .Change value to trigger initial execution.
    }

    protected override void OnStop()
    {
        this.Scheduler.Dispose();
    }

    private void ProcessingFunction(object e)
    {
        // Do stuff here
        // Calculate and set next execution time with .Change
    }
}

See also Timers Microsoft documentation.

Upvotes: 1

Fildor
Fildor

Reputation: 16084

Assuming the Service is not only for this "once-a-month-work" but provides an actual service beyond that, I would:

  1. Add a Custom Command (See Dokumentation )

  2. Write a Script or Short Program that does nothing else, but trigger that command.

  3. Use Windows Task Scheduler to have that Script/Program be executed according to the desired schedule.

Pros:

  • You can keep most of your existing code mostly unchanged.
  • You have a "real" scheduler managed by windows
    • Reboots: check
    • Configurable Error handling: check
    • Leap years considered: check
    • Daylight Savings considered: check
    • ...

EDIT

If that "once-a-month-work" is the only purpose of that Service, then this is not a suitable Application Format. In this case, I'd make it a Console app and have the Windows Task Scheduler schedule and run it. You have all the "pros" and get rid of Service management.

Upvotes: 1

mm8
mm8

Reputation: 169200

Using the Task Scheduler in Windows is a good and common option.

The other option is to use a scheduler library like for example FluentScheduler. This requires your app to be up an running during the whole month though so I would go with the task scheduler and let it simply run the code to be executed and then let the process finish.

Suspending a thread or burning CPU cycles for a month (!) is nonsense so forget about 1) and 2) and probably also about using FluentScheduler if your process isn't mean to be truly long-running.

Upvotes: 3

Related Questions