Jb11281992 Jb11281992 - 3 months ago 8
C# Question

Why is my timer not initiating in my Web Form project?

When I run the program, there is no console output. I have attempted a few other timer procedures and to no avail.

Code Snippet:

namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());


System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 3000;
aTimer.Enabled = true;

}

private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("Hello World!");
}
}
}

Answer

Application.Run(new Form1()); is a blocking call. It basically just sits there until you close your application.

https://msdn.microsoft.com/en-us/library/system.windows.forms.application.run(v=vs.110).aspx

To check it out, put a break point on the entry to the Main() function, and debug-step through your application!

Comments