1

I have created a windows service.In which I have set the timer interval for every one minute and so it was triggering for every minute,. But I need to trigger the same for each day..

You can find the code below where I have set the timer interval in OnStart() method..

Code:

    protected override void OnStart(string[] args)
    {
        TraceService("start service");

        //handle Elapsed event
        timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);

        //This statement is used to set interval to 1 minute (= 60,000 milliseconds)

        timer.Interval = 86400000;

        //enabling the timer
        timer.Enabled = true;
    }
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
D.N.E.Z
  • 63
  • 1
  • 8

3 Answers3

5

This post is quite old but I think it's worth mentioning that instead of using a magic number like 86400000 it's better to use something like new TimeSpan(1, 0, 0, 0).TotalMilliseconds , so that if someone needs to change it they will know what should be changed.

Andrey Gubal
  • 3,481
  • 2
  • 18
  • 21
Aref Karimi
  • 1,822
  • 4
  • 27
  • 46
0

So, couple of things regarding your post:

First, it does not state what is the problem!

Technically, Timer supports a timer interval of about 25 days. So, your code should work.

If you want to exceed 25 days and you are not concerned about thread safety, I suggest you go to System.Threading.Timer.

More info here

Since, your service runs as a windows based service, i suggest you go to System.Threading.Timer.

Community
  • 1
  • 1
  • My question is how to set the timer interval for a day.. When I try giving for a minute(60000 millisecs) its working.. Hence I gave 86400000(24*60*60*1000) as the timer interval which is not working.. I was testing by calling a method which stores the log entries in a seperate file where I am not getting the entries when I check for a a day just by changing the System date time manually... Please provide solution to achieve this... – D.N.E.Z Aug 29 '13 at 07:34
  • @D.N.E.Z - if you tell a timer to wait for a day, it will wait for a day. It doesn't pay attention to the system clock. – Damien_The_Unbeliever Aug 29 '13 at 10:03
0

In C# now you can use TimeSpan class to get total milliseconds in day to set a per day timer.

var totalMilliSecondsPerDay = TimeSpan.FromDays(1).TotalMilliseconds;
var timer = new Timer(totalMilliSecondsPerDay);