Sachin Rathore

Sachin Rathore

  • 1.2k
  • 153
  • 1.4k

How to use a countdown timer for one page server only for (2 :55)

Oct 1 2020 12:52 AM
  1. <asp:ScriptManager ID="ScriptManager1" runat="server">  
  2. </asp:ScriptManager>  
  3. <asp:UpdatePanel ID="UpdatePanel1" runat="server">  
  4. <ContentTemplate>  
  5. <asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick">  
  6. </asp:Timer>  
  7. <asp:Label ID="Label1" runat="server" Text=""></asp:Label>  
  8. </ContentTemplate>  
  9. </asp:UpdatePanel>  
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3. if (Session["CountdownTimer"] == null)  
  4. {  
  5. Session["CountdownTimer"] = new CountDownTimer(TimeSpan.Parse("00:02:55"));  
  6. (Session["CountdownTimer"as CountDownTimer).Start();  
  7. }  
  8. }  
  9. protected void Timer1_Tick(object sender, EventArgs e)  
  10. {  
  11. if (Session["CountdownTimer"] != null)  
  12. {  
  13. Label1.Text = (Session["CountdownTimer"as CountDownTimer).TimeLeft.ToString();  
  14. string GetTime = Label1.Text;  
  15. string[] Array = GetTime.Split(':');  
  16. string Minutes = Array[1];  
  17. string Seconds = Array[2];  
  18. if (Convert.ToInt32(Minutes) == 00 && Convert.ToInt32(Seconds) == 30)  
  19. {  
  20. //boxid.Enabled = false;  
  21. //boxid.ReadOnly = true;  
  22. }  
  23. else if (Convert.ToInt32(Minutes) == 0 && Convert.ToInt32(Seconds) == 0)  
  24. {  
  25. Session["CountdownTimer"] = new CountDownTimer(TimeSpan.Parse("00:02:55"));  
  26. (Session["CountdownTimer"as CountDownTimer).Start();  
  27. //boxid.Enabled = true;  
  28. //boxid.ReadOnly = false;  
  29. }  
  30. }  
  31. }  
  32. public class CountDownTimer  
  33. {  
  34. public TimeSpan TimeLeft;  
  35. System.Threading.Thread thread;  
  36. public CountDownTimer(TimeSpan original)  
  37. {  
  38. this.TimeLeft = original;  
  39. }  
  40. public void Start()  
  41. {  
  42. // Start a background thread to count down time  
  43. thread = new System.Threading.Thread(() =>  
  44. {  
  45. while (true)  
  46. {  
  47. System.Threading.Thread.Sleep(1000);  
  48. TimeLeft = TimeLeft.Subtract(TimeSpan.Parse("00:00:01"));  
  49. }  
  50. });  
  51. thread.Start();  
  52. }  
  53. }  
I am using this code but it is not working according to my requirement.
 
My Requirement the countdown timer for eg:- Timer is open 10 devices and 20 devices(mobile and laptop from differenr I.P.). the timer is same all the time. run only 2 minute 55 second (5 second pause) is it possible so please help me (ASP.NET C#)

Answers (2)