Reputation: 1
I just want to create WindowsFormsApp that timer run when program start and then textbox2 will show text
event run
EDIT
timer tick but
textBox2.Text = "event run"; << Doesn't Run
{
private static System.Timers.Timer myTimer;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
myTimer = new System.Timers.Timer(5000);
myTimer.Elapsed += myEvent;
myTimer.AutoReset = true;
myTimer.Enabled = true;
textBox1.Text = "Timer start";
}
private void myEvent(Object source, System.Timers.ElapsedEventArgs e)
{
textBox2.Text = "event run";
}
}
Can anyone help? Thanks!
EDIT
I add myTimer.Start(); and breakpoint hit in myEvent but textbox doesn't show massage
Upvotes: 0
Views: 83
Reputation: 331
You did not start the timer myTimer.Start();
that's why timer is not working.
myTimer.Interval = 1000;
myTimer.Start();
And for more details, refer to this.
Upvotes: 2
Reputation: 2927
You have not called Start
method like this
private void Form1_Load(object sender, EventArgs e)
{
myTimer = new System.Timers.Timer(5000);
myTimer.Elapsed += myEvent;
myTimer.AutoReset = true;
myTimer.Enabled = true;
myTimer.Start();
textBox1.Text = "Timer start";
}
You have just enabled timer and all but you need to start it also using Start
method.
Also there is no need for static
keyword as then you cannot access it in Form_Load
which is not static
Upvotes: 1