Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Thread

Thread

Scheduled Pinned Locked Moved ASP.NET
csharphelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Munteanu Ciprian
    wrote on last edited by
    #1

    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??

    A 1 Reply Last reply
    0
    • M Munteanu Ciprian

      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??

      A Offline
      A Offline
      Abhishek Sur
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups