Invisible countdown?
-
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.
jafingi wrote:
I want to run a function every minute,
It is called a Timer Control. Google has a lot of examples[^]
-
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.
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] }
-
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.
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
-
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