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. General Programming
  3. C#
  4. Invisible countdown?

Invisible countdown?

Scheduled Pinned Locked Moved C#
tutorialquestion
5 Posts 4 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.
  • J Offline
    J Offline
    jafingi
    wrote on last edited by
    #1

    Hi, Can anyone make a quick example that shows how to countdown in time? I want to run a function every minute, so it needs to countdown in the background with no textoutput (where it shows the timer). Thanks.

    T L M 3 Replies Last reply
    0
    • J jafingi

      Hi, Can anyone make a quick example that shows how to countdown in time? I want to run a function every minute, so it needs to countdown in the background with no textoutput (where it shows the timer). Thanks.

      T Offline
      T Offline
      Tarakeshwar Reddy
      wrote on last edited by
      #2

      jafingi wrote:

      I want to run a function every minute,

      It is called a Timer Control. Google has a lot of examples[^]

      1 Reply Last reply
      0
      • J jafingi

        Hi, Can anyone make a quick example that shows how to countdown in time? I want to run a function every minute, so it needs to countdown in the background with no textoutput (where it shows the timer). Thanks.

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, you would use some kind of timer: - if the periodic function is lightweight and needs to access the GUI (assuming a Windows app), the easiest solution is a Windows.Forms.Timer - if the periodic function takes more than say 50 msec, you should use a Timers.Timer or Threading.Timer; it will execute its handler on a different thread, hence not blocking the GUI. But if it also needs to access the GUI, you will have to use the Control.InvokeRequired and Control.Invoke() pattern. Just one example. somewhere, maybe in the form's constructor:

        Windows.Forms.Timer timer=new Windows.Forms.Timer();
        timer.Interval=60000; // that's about one minute
        timer.Tick+=new EventHandler(timer\_Tick);
        timer.Start();
        

        And this is the method that will execute periodically; you may not use the parameters, but they must be there anyway.

        void timer\_Tick(object sender, EventArgs e) {
        	Console.WriteLine("whatever");
        	myTextBox.Text=DateTime.Now.ToString("hh:mm:ss");
        }
        

        Warning: most timers vanish (i.e. could be garbage collected) as soon as you stop them, unless of course you keep a reference to it (e.g. as a class member, rather than a local variable). :)

        Luc Pattyn


        try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


        1 Reply Last reply
        0
        • J jafingi

          Hi, Can anyone make a quick example that shows how to countdown in time? I want to run a function every minute, so it needs to countdown in the background with no textoutput (where it shows the timer). Thanks.

          M Offline
          M Offline
          Manoj Kumar Rai
          wrote on last edited by
          #4

          For the very precise intervals you can creat a worker thread and with the help of Thread.Sleep() function you can do the countdown.

          Manoj Never Gives up

          J 1 Reply Last reply
          0
          • M Manoj Kumar Rai

            For the very precise intervals you can creat a worker thread and with the help of Thread.Sleep() function you can do the countdown.

            Manoj Never Gives up

            J Offline
            J Offline
            jafingi
            wrote on last edited by
            #5

            Thanks to all :) Works perfect!

            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