3

I want to write a windows service. But it needs to run at specific times. For example, my service should email me every 5 minutes.

private Timer _timer;
private DateTime _lastRun = DateTime.Now;

protected override void OnStart(string[] args)
{
    _timer = new Timer(10 * 60 * 1000); // every 10 minutes
    _timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
    //...
}


private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    // ignore the time, just compare the date
    if (_lastRun.Date < DateTime.Now.Date)
    {
        // stop the timer while we are running the cleanup task
        _timer.Stop();
        //
        // do cleanup stuff
        //
        _lastRun = DateTime.Now;
        _timer.Start();
    }
}

I want my service to email me every 5 minutes.

live-love
  • 34,372
  • 16
  • 163
  • 152
Ersin Gülbahar
  • 6,344
  • 14
  • 54
  • 105
  • I don't think this is enough information to be honest. It's really hard to provide anything helpful with no code. [Try this](http://stackoverflow.com/questions/503564/how-might-i-schedule-a-c-sharp-windows-service-to-perform-a-task-daily) – yoonsi Jul 16 '12 at 08:53
  • This question been asked, here is the link. You use this System.Threading.Timer this is the key to success http://stackoverflow.com/questions/246697/windows-service-and-timer – IamStalker Jul 16 '12 at 08:55
  • I want to mail me , ı edit my question – Ersin Gülbahar Jul 16 '12 at 08:55

8 Answers8

5

Well I have seen another question which discusses windows service vs scheduled task also this. So you might want to have a look at that other than that here is 2 similar questions which might help:

here too are some great links:

Community
  • 1
  • 1
user1518101
  • 111
  • 3
  • And run at `7:00 a.m to 21:00 pm` interval each day ***Monday To Friday*** ? ***Not eastern-holidays*** – Kiquenet Mar 21 '18 at 17:05
2

Take a look at Quartz.NET or search for "Windows Task Scheduling".

The first approach is programmatic and the later is configuration.

oleksii
  • 33,544
  • 11
  • 83
  • 149
1

Why not just add a timer to your service who does what you want on the interval you configure?

Gerald Versluis
  • 21,913
  • 5
  • 52
  • 75
1

The article gives a walkthorugh how to creat windows Service http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.80).aspx

And to run it at a certain time you can set schedule job

HatSoft
  • 10,683
  • 3
  • 25
  • 43
1

You might want to use something like Quartz.net in your service. You can then use cron expressions to schedule "jobs" within your service.

manojlds
  • 259,347
  • 56
  • 440
  • 401
1

Your code looks good, but did you make a call to timer.start() in the first place during OnStart?

KingCronus
  • 4,440
  • 1
  • 21
  • 46
1

If you need to do an operation on a timer, then you have two choices

  1. Create a Windows Service and implement some kind of scheduling. This can be complex, so be prepared to that you may spend a lot of time solving issues that have already been solved.
  2. You could setup a scheduled task instead that calls your application.

It all depends how much control you really need.

Kieron
  • 24,968
  • 14
  • 72
  • 113
0

For the above said functionality, you can use the combination of console application with windows scheduler.

Schedule your exe to execute every 5 mins. Your exe will email you and stop. Is there anything specific you want to achieve through windows service?

Estefany Velez
  • 328
  • 4
  • 18