Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timer.Interval to run the function for the first time also

I have created one Windows Service where I need to execute the task on every day basis. so to accomplish that I have used Timer control.

Timer tm = new Timer();
tm.Elapsed += new ElapsedEventHandler(OnElapsedTime);
tm.Interval = 86400000;

But here the problem is, after I start the service, I have to wait for an entire 1 day to actually call that function.

So Is there a way where the function just been call at a time I start the service and don't have to wait till the interval time finishes.?

Thanks in advance.

like image 695
Vishal Suthar Avatar asked Jun 01 '13 06:06

Vishal Suthar


People also ask

What is timer interval in C#?

C# Timer is used to implement a timer in C#. The Timer class in C# represents a Timer control that executes a code block at a specified interval of time repeatedly. For example, backing up a folder every 10 minutes, or writing to a log file every second.

Does timer control have a UI or graphical representation?

Use of Timer Control A Timer control does not have a visual representation and works as a component in the background.

How do I make a countdown timer in C#?

Count Down Timer With the Timer Class in C# The Timer class) is used to execute a function inside a separate thread in C#. We can use the Timer function to create a count-down timer in C#. The Timer. Interval property sets the interval between each tick of the timer in milliseconds.


2 Answers

You can run the method first time and then start the timer. So it will do the fist run and timer will run again in next day.

Or Create simple console application to do the task. simply by using windows scheduled task you can run this exe daily

Another option is call the event immediately after start the timer

How to fire timer.Elapsed event immediately

like image 68
Damith Avatar answered Sep 30 '22 13:09

Damith


There are 2 ways of doing it.

1) The code that is there in the OnElapsedTime, put that in another method and call that method as soon as you start the timer. For eg, Put the code in TimerCalled method and then use this:

static void Main(string[] args)
{
    Timer tm = new Timer();
    tm.Elapsed += new ElapsedEventHandler(OnElapsedTime);
    tm.Interval = 86400000;
    TimerCalled();
}

private static void OnElapsedTime(object source, ElapsedEventArgs e)
{
    TimerCalled();
}

2)You can use the System.Threading.Timer class instead. You can use it like this:

System.Threading.Timer tm = 
    new System.Threading.Timer(OnElapsedTime, null, 0, 86400000);

But be aware that the System.Threading.Timer runs on a separate thread. Hence any attempt to update the UI elements from that thread would result in error. You should rather delegate the UI elements updates, if any, to the UI thread via Dispatcher or some other means.

UPDATE: If you want to start a few minutes after the service starts, you could provide a delay of couple of minutes:

System.Threading.Timer tm = 
    new System.Threading.Timer(OnElapsedTime, null, 120, 86400000);
like image 20
Shakti Prakash Singh Avatar answered Sep 30 '22 14:09

Shakti Prakash Singh