Answered by:
Timer elapsed event not fired

Question
-
Hi all
I just want to call a function repeatedly with a time interval. However, the elapsed event is not fired. Can anyone of you comment on that, what sould be tje reason.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Timers; namespace TimerDemo { class Program { static void Main(string[] args) { Timer m_Timer; m_Timer = new Timer(6000); m_Timer.Elapsed += new ElapsedEventHandler(TimerElapsed); m_Timer.Enabled = true; } static void TimerElapsed(object sender, ElapsedEventArgs e) { new Program().PrintMsg(); } void PrintMsg() { Console.WriteLine("kick"); } } }
Thanks in advanceFriday, February 10, 2012 7:47 AM
Answers
-
The problem is your console application is getting closed before the timer event happening.
Use the following code to see the message.
Timer m_Timer;
m_Timer = new Timer(6000);
m_Timer.Elapsed += new ElapsedEventHandler(TimerElapsed);
m_Timer.Enabled = true;
Console.ReadKey();
Resolving n Evolving in C# (http://jeanpaulva.com)
- Marked as answer by lookingtobe Friday, February 10, 2012 8:17 AM
Friday, February 10, 2012 8:08 AM
All replies
-
You forgot to call the timer .start
m_Timer.Start();
--------------------------------------------------------
Surender Singh Bhadauria
My Blog- Edited by Surender Singh Bhadauria Friday, February 10, 2012 7:50 AM
Friday, February 10, 2012 7:50 AM -
Hi,
your timer should have a greater scope I think:
class Program { Timer m_Timer; m_Timer = new Timer(6000); static void Main(string[] args) { m_Timer.Elapsed += new ElapsedEventHandler(TimerElapsed); m_Timer.Enabled = true; } ... }
also,
i think, if there's no form, your program will end before the first elapsed event occurs, because there's nothing to keep it running.
Regards, Nico
Friday, February 10, 2012 8:04 AM -
The problem is your console application is getting closed before the timer event happening.
Use the following code to see the message.
Timer m_Timer;
m_Timer = new Timer(6000);
m_Timer.Elapsed += new ElapsedEventHandler(TimerElapsed);
m_Timer.Enabled = true;
Console.ReadKey();
Resolving n Evolving in C# (http://jeanpaulva.com)
- Marked as answer by lookingtobe Friday, February 10, 2012 8:17 AM
Friday, February 10, 2012 8:08 AM -
You forgot to call the timer .start
m_Timer.Start();
--------------------------------------------------------
Surender Singh Bhadauria
My Blog
I don't think mate. Because, as the documentation explain both Start() and Enable() does the same.Friday, February 10, 2012 8:14 AM -
The problem is your console application is getting closed before the timer event happening.
Use the following code to see the message.
Timer m_Timer;
m_Timer = new Timer(6000);
m_Timer.Elapsed += new ElapsedEventHandler(TimerElapsed);
m_Timer.Enabled = true;
Console.ReadKey();
Resolving n Evolving in C# (http://jeanpaulva.com)
Oh yes, this is what exactly happens. Thanks a lot.Friday, February 10, 2012 8:16 AM -
Hi,
your timer should have a greater scope I think:
class Program { Timer m_Timer; m_Timer = new Timer(6000); static void Main(string[] args) { m_Timer.Elapsed += new ElapsedEventHandler(TimerElapsed); m_Timer.Enabled = true; } ... }
also,
i think, if there's no form, your program will end before the first elapsed event occurs, because there's nothing to keep it running.
Regards, Nico
However, Timer object cannot initialize at that scope right?Friday, February 10, 2012 8:17 AM -
As Jean Paul said, your program immediately closes, so you don't get any output. Add Console.ReadKey or Console.ReadLine statement at the end, so that you main program doesn't terminate. Also, I hope you are waiting 6 seconds to get the output :)
Please mark this post as answer if it solved your problem. Happy Programming!
Friday, February 10, 2012 8:29 AM