Thread
-
Hi! Here what I want to do: I have a label and in it I want to display the seconds beginning from 60 and finishing with 0 at the interval of one second, and then again and again (basically a little stop watch). I've tried something using C# and a delegate, but I can't use the method Invoke which I was using in C#. Here the of what I've done:
public delegate void del_time();
public Thread threadTime;
public int time = 60;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
labelTime.Text = "TIME 60";
threadTime = new Thread(new ThreadStart(Display_Time));
threadTime.IsBackground = true;
threadTime.Start();
}
}protected void Dsiplay_Time()
{
del_time del1 = new del_time(Lbl_time);
Thread.Sleep(1000);
while (true)
invoke(Lbl_time, new object[] { });
}protected void Lbl\_timp() { if (time > 0) { time -= 1; labelTime.Text = "TIME " + time.ToString(); } else { time = 60; labelTime.Text = "TIME " + time.ToString(); } }
Now can anyone tell me if I can do the stop watch this way, or could anyone give any other idea. I've noticed that I have a control called "Timer". I tried to use it, but I get the following error: "The control with ID 'Timer1' requires a ScriptManager on the page. The ScriptManager must appear before any controls that need it." Any ideas??
-
Hi! Here what I want to do: I have a label and in it I want to display the seconds beginning from 60 and finishing with 0 at the interval of one second, and then again and again (basically a little stop watch). I've tried something using C# and a delegate, but I can't use the method Invoke which I was using in C#. Here the of what I've done:
public delegate void del_time();
public Thread threadTime;
public int time = 60;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
labelTime.Text = "TIME 60";
threadTime = new Thread(new ThreadStart(Display_Time));
threadTime.IsBackground = true;
threadTime.Start();
}
}protected void Dsiplay_Time()
{
del_time del1 = new del_time(Lbl_time);
Thread.Sleep(1000);
while (true)
invoke(Lbl_time, new object[] { });
}protected void Lbl\_timp() { if (time > 0) { time -= 1; labelTime.Text = "TIME " + time.ToString(); } else { time = 60; labelTime.Text = "TIME " + time.ToString(); } }
Now can anyone tell me if I can do the stop watch this way, or could anyone give any other idea. I've noticed that I have a control called "Timer". I tried to use it, but I get the following error: "The control with ID 'Timer1' requires a ScriptManager on the page. The ScriptManager must appear before any controls that need it." Any ideas??
Well my friend, the process you are using is actually creates one thread in the server side. You need the timer to work in the client side. In disconnected env (Like Web) whenever a Response is transferred to the client, anything in the server regarding that request disposes. Whenever we call the server again, it will actually create another Response altogether. Use javacript SetTimeOut/ SetInterval / ClearTimeout to deal with this. You call like this :
function UpdateStopWatch(){
...
// Code that updates UI element for the stop watch
...
//Also place ClearTimeout to terminate the loop.
}body onload="javascript:SetInterval('UpdateStopWatch', 1000);"
Read about Javascript timers here http://www.java-scripts.net/javascripts/Countdown-Timer.phtml[^] :)Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->** Simplify Code Using NDepend
Basics of Bing Search API using .NET
Microsoft Bing MAP using Javascript