My timer doesn't work

T

Tony Johansson

Hello!

I have a completely empty form and the class Form1 below and class Program
shown below.
Now I have created a Timer and can't understand why my event handler
timer_Elapsed for the timer isn't trigged.

public partial class Form1 : Form
{
System.Timers.Timer timer = new System.Timers.Timer();

public Form1()
{
InitializeComponent();

timer.Elapsed += new
System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Interval = 1000;
Enabled = true;
}

void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
MessageBox.Show("Testing");
}
}

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());
}
}

Tony
 
S

Scott Seligman

Tony Johansson said:
I have a completely empty form and the class Form1 below and class Program
shown below.
Now I have created a Timer and can't understand why my event handler
timer_Elapsed for the timer isn't trigged.
[...]
Enabled = true;

You need timer.Enabled = true; here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top