B
Borr
Code fragment as simple as possible :
public class TestTimer : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Timers.Timer timer1;
private void Page_Load(
object sender,
System.EventArgs e)
{
Label1.Text = "Before timer";
}
private void timer1_Elapsed(
object sender,
System.Timers.ElapsedEventArgs e)
{
Label1.Text = "After timer";
timer1.Stop();
}
private void InitializeComponent()
{
this.timer1 = new System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)
(this.timer1)).BeginInit();
this.timer1.Enabled = true;
this.timer1.Interval = 5000;
this.timer1.Elapsed += new
System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)
(this.timer1)).EndInit();
}
}
When the timer is elapsed, timer1_elapsed method is called
(I see it in the debugger), line
Label1.Text = "After timer";
runs without any problem, but in fact Label1
remains "Before timer" ! WHY ?
Label1 and timer1 were added automatically, with Web form
designer of Visual Studio .Net, InitializeComponent()
method was generated automatically, and I did not touch it.
public class TestTimer : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Timers.Timer timer1;
private void Page_Load(
object sender,
System.EventArgs e)
{
Label1.Text = "Before timer";
}
private void timer1_Elapsed(
object sender,
System.Timers.ElapsedEventArgs e)
{
Label1.Text = "After timer";
timer1.Stop();
}
private void InitializeComponent()
{
this.timer1 = new System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)
(this.timer1)).BeginInit();
this.timer1.Enabled = true;
this.timer1.Interval = 5000;
this.timer1.Elapsed += new
System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)
(this.timer1)).EndInit();
}
}
When the timer is elapsed, timer1_elapsed method is called
(I see it in the debugger), line
Label1.Text = "After timer";
runs without any problem, but in fact Label1
remains "Before timer" ! WHY ?
Label1 and timer1 were added automatically, with Web form
designer of Visual Studio .Net, InitializeComponent()
method was generated automatically, and I did not touch it.