I want the server to desplay some text on a webpage.
I defined
<asp:Label id="label1" runat="server" />
and set
protected void Button1_Click(object sender, EventArgs e)
{
label1.Text = "bla";
timer = new System.Threading.Timer(new TimerCallback(DoSomething), null, 0, 10000);
}
private void DoSomething(object obj)
{
label1.Text = "bla 1";
}
bla is presented, but not bla1.
When I debug I see the “bla 1” line is executed.
Where do I do wrong?
By the time DoSomething() gets executed, the response has already been sent to the browser, and your code does nothing. Actually, I’m surprised you don’t crash the app domain with it.
You should not attempt to use threading primitives like this in an asp.net application.
If you want to update the value sometime “later” than the initial page displaying, you’ll need to look at Ajax.