Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends,

Hello Friends, I am working on a timer based project. I need that the timer should elapse just once. I wrote the following code:


C#
class Program
   {
       static void Main(string[] args)
       {

           Start_Event_Timer("Rahul");
           Start_Event_Timer("Varinder");


           Console.ReadLine();
       }

       static Timer t;
       public static void Start_Event_Timer(string s1)
       {
           Console.WriteLine("Timer started for : {0} at {1}", s1, DateTime.Now.ToString());
           t = new System.Timers.Timer(60 * 1000);
           t.Enabled = true;
           t.Elapsed += (sender, e) => G_TIMER_ELAPSED(sender, e, s1);

           t.Start();
       }


       public static void G_TIMER_ELAPSED(object sender, ElapsedEventArgs e, string s1)
       {
           Console.WriteLine("Timer elapsed for :{0} at {1} ", s1, DateTime.Now.ToString());

           t.AutoReset = false;
           t.Enabled = false;
           t.Stop();
       }

     }



I will also show you the output which i captured from the console:


Quote:
Timer started for : Rahul at 4/25/2014 11:46:04 AM
Timer started for : Varinder at 4/25/2014 11:46:04 AM
Timer elapsed for :Rahul at 4/25/2014 11:47:04 AM
Timer elapsed for :Varinder at 4/25/2014 11:47:04 AM
Timer elapsed for :Rahul at 4/25/2014 11:48:04 AM
Timer elapsed for :Rahul at 4/25/2014 11:49:04 AM
Timer elapsed for :Rahul at 4/25/2014 11:50:04 AM
Timer elapsed for :Rahul at 4/25/2014 11:51:04 AM




Any suggestions? as to why is the timer being elapsed multiple times even if i started it once?
Posted

You are using one location, the static field t, to store two timer instances. The timer accessible via t is the last one assigned, "Varinder". Although the code is awful, it is possible to make it work as intended, like this :
C#
public static void G_TIMER_ELAPSED(object sender, ElapsedEventArgs e, string s1)
{
    Timer t = (Timer)sender;    
    Console.WriteLine("Timer elapsed for :{0} at {1} ", s1, DateTime.Now.ToString());

    t.AutoReset = false;
    t.Enabled = false;
    t.Stop();
}


The sender parameter in the event handler contains the invoking instance and so simply casting the object gets the correct Timer.

Alan.
 
Share this answer
 
Comments
Rahul VB 25-Apr-14 5:50am    
"awful" is an understatement. The code is bad. It was just a test code. I wanted to check the elapse frequency and proper elapsed time. And yes i did take the timer inside, thanks for that. It worked. How stupid of me.
http://social.msdn.microsoft.com/Forums/vstudio/en-US/03e83a93-652f-4559-ba9e-f397b1755ad7/timerenabled-vs-timerstartstop-?forum=csharpgeneral
 
Share this answer
 
Comments
Rahul VB 25-Apr-14 5:51am    
Thanks for help

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