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. Adding a delay in a loop

Adding a delay in a loop

Scheduled Pinned Locked Moved C#
csharpquestion
7 Posts 5 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
    jctk
    wrote on last edited by
    #1

    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?

    L S 2 Replies Last reply
    0
    • J jctk

      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?

      L Offline
      L Offline
      Luis Alonso Ramos
      wrote on last edited by
      #2

      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!

      C 1 Reply Last reply
      0
      • J jctk

        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?

        S Offline
        S Offline
        S Senthil Kumar
        wrote on last edited by
        #3

        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

        C 1 Reply Last reply
        0
        • S S Senthil Kumar

          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

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          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 1 Reply Last reply
          0
          • C Colin Angus Mackay

            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 Offline
            S Offline
            S Senthil Kumar
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • L Luis Alonso Ramos

              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!

              C Offline
              C Offline
              cishi_us
              wrote on last edited by
              #6

              how the timer can be used???????????????????????? It has lot of classes>>>>and members please tell something in detail

              L 1 Reply Last reply
              0
              • C cishi_us

                how the timer can be used???????????????????????? It has lot of classes>>>>and members please tell something in detail

                L Offline
                L Offline
                Luis Alonso Ramos
                wrote on last edited by
                #7

                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 the Tick 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!

                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