Adding a delay in a loop
-
I want to add a delay at the end of my while loop, so that it executes once every 10 minutes but am unsure how that works in C#. I've tried Thread.Sleep, but it doesnt delay the loop. Any suggestions?
Use a timer, and when 10 minutes have elapsed, start your processing. -- LuisR
Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!
-
I want to add a delay at the end of my while loop, so that it executes once every 10 minutes but am unsure how that works in C#. I've tried Thread.Sleep, but it doesnt delay the loop. Any suggestions?
Thread.Sleep should work. What did you pass as the parameter? For 10 minutes, it should be 10 * 60 * 1000. But remember that Thread.Sleep would be less efficient than a signaling mechanism as Thread.Sleep would cause the Thread to use up CPU time... Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Thread.Sleep should work. What did you pass as the parameter? For 10 minutes, it should be 10 * 60 * 1000. But remember that Thread.Sleep would be less efficient than a signaling mechanism as Thread.Sleep would cause the Thread to use up CPU time... Regards Senthil _____________________________ My Blog | My Articles | WinMacro
S. Senthil Kumar wrote: Thread.Sleep would cause the Thread to use up CPU time Really? My understanding was that it blocked the thread for the specified amount of time so the side effect is that other threads cannot interact with it.
My: Blog | Photos | Next SQL Presentation WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More
-
S. Senthil Kumar wrote: Thread.Sleep would cause the Thread to use up CPU time Really? My understanding was that it blocked the thread for the specified amount of time so the side effect is that other threads cannot interact with it.
My: Blog | Photos | Next SQL Presentation WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More
Thread.Sleep() causes "busy wait", where the thread uses up its quantum of CPU time doing nothing. Waiting for a signal (or in general, any OS synchronization mechanism) causes the thread to be context switched for another immediately. This obviously results in performance benefits as other threads get a chance to run in the time that a "sleeping" thread would have spent doing nothing. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Use a timer, and when 10 minutes have elapsed, start your processing. -- LuisR
Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!
-
how the timer can be used???????????????????????? It has lot of classes>>>>and members please tell something in detail
It's actually very easy. There are two kinds of timers in the .NET Framework:
- Windows Forms timers (
System.Windows.Forms.Timer
class), which you drag in the desinger to your form, set some properties, and write the code for theTick
event handler, and that's it. For your situation, the problem is that if the process is long, it will block your UI thread and your main form will not be responsible. - Thread timer (
System.Threading.Timer
). I have never used this one, but from the docs[^] it seems you set a timer, without requiring a form, that will run on another thread. So, your form keeps working normally while another thread does your processing on the background.
I believe the second one will suit your purpose better. But, as I have never used a
System.Threading.Timer
, I can't be of more help, since all I know is the same as you, what is included in the documentation. If someone knows better, feel free to correct me please! :) Good luck! -- LuisR
Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!
- Windows Forms timers (