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. When should i use ?? Timer vs Thread.Sleep(int);

When should i use ?? Timer vs Thread.Sleep(int);

Scheduled Pinned Locked Moved C#
visual-studiotutorialquestioncareer
7 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.
  • A Offline
    A Offline
    Abdul Gafoor
    wrote on last edited by
    #1

    Hi, When should i use ?? Timer vs Thread.Sleep(int); could u please guide me with justification? i believe that : I can prefer Thread.Sleep(); if my execution has to be blocked for a specific amount time, and go for Timer if i have to execute a specific job after a period. Is it like that???? Or Anything else and more important is there? please guide me.. Thanks

    L 1 Reply Last reply
    0
    • A Abdul Gafoor

      Hi, When should i use ?? Timer vs Thread.Sleep(int); could u please guide me with justification? i believe that : I can prefer Thread.Sleep(); if my execution has to be blocked for a specific amount time, and go for Timer if i have to execute a specific job after a period. Is it like that???? Or Anything else and more important is there? please guide me.. Thanks

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

      Hi, it is slightly more complex: - you can use timers to get a single activation in future, or a periodic activation; - the timer fires its event on some thread; for a Windows.Forms.Timer that would be the main or GUI thread; for other timers, it is a different thread (which is good since it does not load the GUI thread, but it also makes you need Control.InvokeRequired and Controle.Invoke() if the timer's handler needs to touch some Control). - you can use Thread.Sleep() to put the current thread to sleep for some time, but only if that is acceptable to the thread. It is probably OK to do it in a worker thread or background thread, and not in the main thread (aka "GUI thread") since during the sleep the entire GUI comes to a halt, so it would not repaint when uncovered, you would not be able to resize it, etc etc. So, if you have things to do on the GUi thread thar must be kept apart in time, you have to organize that without Trhead.Sleep(); in that case a Windows.Forms.Timer is the right choice. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/AllLanguages/General - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


      P 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, it is slightly more complex: - you can use timers to get a single activation in future, or a periodic activation; - the timer fires its event on some thread; for a Windows.Forms.Timer that would be the main or GUI thread; for other timers, it is a different thread (which is good since it does not load the GUI thread, but it also makes you need Control.InvokeRequired and Controle.Invoke() if the timer's handler needs to touch some Control). - you can use Thread.Sleep() to put the current thread to sleep for some time, but only if that is acceptable to the thread. It is probably OK to do it in a worker thread or background thread, and not in the main thread (aka "GUI thread") since during the sleep the entire GUI comes to a halt, so it would not repaint when uncovered, you would not be able to resize it, etc etc. So, if you have things to do on the GUi thread thar must be kept apart in time, you have to organize that without Trhead.Sleep(); in that case a Windows.Forms.Timer is the right choice. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/AllLanguages/General - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        Not fair. I wanted to answer this one;P. Good answer though.

        Deja View - the feeling that you've seen this post before.

        M 1 Reply Last reply
        0
        • P Pete OHanlon

          Not fair. I wanted to answer this one;P. Good answer though.

          Deja View - the feeling that you've seen this post before.

          M Offline
          M Offline
          Malcolm Smart
          wrote on last edited by
          #4

          You answered like this because it seems you have no idea about the subject.:laugh:

          "More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF

          P 1 Reply Last reply
          0
          • M Malcolm Smart

            You answered like this because it seems you have no idea about the subject.:laugh:

            "More functions should disregard input values and just return 12. It would make life easier." - comment posted on WTF

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            :laugh:Yup - that's me. I'm the ultimate expert. I know nothing about everything.:laugh:

            Deja View - the feeling that you've seen this post before.

            L 1 Reply Last reply
            0
            • P Pete OHanlon

              :laugh:Yup - that's me. I'm the ultimate expert. I know nothing about everything.:laugh:

              Deja View - the feeling that you've seen this post before.

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

              Pete O`Hanlon wrote:

              I know nothing about everything

              Then why is it I have seen questions you did not answer ?

              Luc Pattyn [Forum Guidelines] [My Articles]


              this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/AllLanguages/General - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


              P 1 Reply Last reply
              0
              • L Luc Pattyn

                Pete O`Hanlon wrote:

                I know nothing about everything

                Then why is it I have seen questions you did not answer ?

                Luc Pattyn [Forum Guidelines] [My Articles]


                this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/AllLanguages/General - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                I like to spread my ignorance thinly.

                Deja View - the feeling that you've seen this post before.

                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