Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to execute a method for every 5 seconds. How to do it with the Timer concept?

I need to know whether the timer runs and hits the method for every 5 seconds. So I tried the below code.

int autoPollingTime = Convert.ToInt32(configparams["AutoPollQueues"]);
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = autoPollingTime; // time in Milliseconds
if (autoPollingTime != 0)
{
timer.Start();
if (timer.Interval == autoPollingTime)
{
Check();
Console.WriteLine("Timer completed");
Thread.Sleep(5000);
}
else
{
return;
}
}

While debugging the code, I should have got the console message for every 5 seconds. But I am not getting it. I do not know what is the problem.

Kindly Help.

What I have tried:

Googled but not getting the exact fix for the issue
Posted
Updated 27-Feb-18 22:47pm

Don't use thread.Sleep - that prevents your UI thread from displaying anything.
And Interval doesn't change as the timer is running - it's the number of ticks between successive timer "activations".

What you need to do is handle the Timer.Elapsed event to find out when the timer has "fired":
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
    Console.WriteLine("Timer elapsed");
    }

private void MyButton_Click(object sender, EventArgs ew)
    {
    System.Timers.Timer timer = new System.Timers.Timer();
    timer.Interval = 5000;
    timer.Elapsed += timer_Elapsed;
    timer.Start();
    }
 
Share this answer
 
Comments
Maciej Los 28-Feb-18 2:51am    
5ed!
You will have to use Timer.Elapsed Event [^]
Timer Class (System.Timers)[^]
refer this example

using System;

namespace CPTemp
{
   public class Class1
    {
       static void Main(string[] args)
       {
           int autoPollingTime = 1000; //Convert.ToInt32(configparams["AutoPollQueues"]);
           System.Timers.Timer timer = new System.Timers.Timer();
           timer.Interval = autoPollingTime;  
           if (autoPollingTime != 0)
           {
               
               timer.Elapsed += timer_Elapsed;
               Console.WriteLine("Timer started...");
               timer.Start();
               Console.ForegroundColor = ConsoleColor.Red;
               Console.WriteLine("Press any key to stop the timer");
               
               Console.ReadKey();
               timer.Stop();
               Console.WriteLine("\n Timer Stopped");
               Console.ReadKey();
           }
       }

       static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
       {
            Check(); 
       }

       private static void Check()
       {
           Console.ForegroundColor = ConsoleColor.White;
           Console.WriteLine("Timer running...");
       }
    }
}
 
Share this answer
 
v2
Comments
Maciej Los 28-Feb-18 2:51am    
5ed!
Karthik_Mahalingam 28-Feb-18 3:03am    
Thank you Maciej
// you can use TimerCallback
C#
Timer timer;
        public void CallFirstTime()
        {        
         
            TimerCallback tmCallback = your_method_name_to_call_for_every_5_second_like(interval_method);
            timer = new Timer(tmCallback, "any parameter", 0, 5000);
        }
 private void interval_method(object _SessionID)
        {
// this method call every 5 second
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900