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. Thread.Sleep is NOT evil

Thread.Sleep is NOT evil

Scheduled Pinned Locked Moved C#
databasecomquestiondiscussioncareer
42 Posts 9 Posters 4 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.
  • B BobJanova

    Why is that better? Actually isn't that worse because when you call WakeAllThreads it will wait on the lock and your app will be unresponsive in all threads (including the one you're sending a wakeup call from)?

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

    That's just a paste of some code I use to wake things up to kill all the threads (this is the scenario I talk about above). Monitor has a simple Pulse method as well to wake a single thread before the timeout expires.

    I was brought up to respect my elders. I don't respect many people nowadays.
    CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

    D 1 Reply Last reply
    0
    • L Lost User

      Let's have the same discussion on;

      • GOTO
      • On Error Resume Next
      • GC.Collect
      • Application.DoEvents

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #15

      Let's not ;)

      Dave
      Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

      D 1 Reply Last reply
      0
      • D Dave Kreskowiak

        I haven't read the article at all, but I'll give you another reason why it's evil. Every once in a while, you can get this little cryptic error message and you've got absolutely no clue as to what it means or why you're getting it: "The CLR has been unable to transition from COM context #x###### to COM context #x###### for 60 seconds. The thread that owns the destination context/apartment...blah blah blah" Why does this happen?? Well, you get this mesage onyl when running under the debugger. Usually it's because Thread.Sleep has been called for greater than 60 seconds or there is a long running operation going on on a thread hosting a message pump. STA COM is accomplished through message passing and if those message don't get processed ... kaboom!

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak

        D Offline
        D Offline
        devvvy
        wrote on last edited by
        #16

        ok, that's if the worker thread has a message pump - if not nothing wrong.

        dev

        D 1 Reply Last reply
        0
        • N N a v a n e e t h

          Simon_Whale wrote:

          What would you recommend as a better method of doing the same as thread.sleep inside a thread?

          If you want to run a job which executes in every 'n' minutes, you can use System.Threading.Timer. If you want to wait till another thread finishes execution, you can use WaitHandle. If you just want to sleep, use Thread.Sleep() :)

          Best wishes, Navaneeth

          D Offline
          D Offline
          devvvy
          wrote on last edited by
          #17

          sure as i pointed out earlier timer/event subscriber - but why bother. THUS FAR there isn't ONE SINGLE argument that justify against use of Thread.Sleep against SCENARIO 2. I hereby invite you synatex lawyers/bitches to put forth your challenge to overthrow this hypothesis.

          dev

          P N 2 Replies Last reply
          0
          • D DaveyM69

            Let's not ;)

            Dave
            Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

            D Offline
            D Offline
            devvvy
            wrote on last edited by
            #18

            why not? we're developers - i love tell others what's right (cool) and what wrong (evil)

            dev

            1 Reply Last reply
            0
            • G GuyThiebaut

              Thanks Pete! Much appreciated :) I shall now use that method - it would be nice if Microsoft deprecated the sleep method so that people like me were prevented from using it.

              “That which can be asserted without evidence, can be dismissed without evidence.”

              ― Christopher Hitchens

              D Offline
              D Offline
              devvvy
              wrote on last edited by
              #19

              why the appreciation? It's more code than a simple while loop with a Sleep. why you want be thankful when there really isn't a justification for it? M$ probably deprecate Thread.Sleep either because its syntax laywer told him so or simply it isn't "cool"

              dev

              P G 2 Replies Last reply
              0
              • D devvvy

                ok, that's if the worker thread has a message pump - if not nothing wrong.

                dev

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #20

                No, it's not. Do you know how times I've seen Thread.Sleep show up on the UI thread?? LOTS!

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak

                D 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  No, it's not. Do you know how times I've seen Thread.Sleep show up on the UI thread?? LOTS!

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak

                  D Offline
                  D Offline
                  devvvy
                  wrote on last edited by
                  #21

                  still - there's nothing against SCENARIO 2, if it's not a UI thread or one with a message pump. Thread.Sleep isn't evil - it's just uncool. It's uncool because most developers tell each other it's uncool to do so.

                  dev

                  D 1 Reply Last reply
                  0
                  • P Pete OHanlon

                    That's just a paste of some code I use to wake things up to kill all the threads (this is the scenario I talk about above). Monitor has a simple Pulse method as well to wake a single thread before the timeout expires.

                    I was brought up to respect my elders. I don't respect many people nowadays.
                    CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                    D Offline
                    D Offline
                    devvvy
                    wrote on last edited by
                    #22

                    there's still no justification to not just do a simple while+Thread.Sleep for SCENARIO 2 if the thread isn't an UI thread (Despite how uncool it has become to Thread.Sleep because syntax lawyers tell us so)

                    dev

                    1 Reply Last reply
                    0
                    • D devvvy

                      why the appreciation? It's more code than a simple while loop with a Sleep. why you want be thankful when there really isn't a justification for it? M$ probably deprecate Thread.Sleep either because its syntax laywer told him so or simply it isn't "cool"

                      dev

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

                      devvvy wrote:

                      why the appreciation? It's more code than a simple while loop with a Sleep.

                      So, you think he has to write this out every time? Chuck it in a library and you're done - a simple

                      new Utility().WaitForTime(5000);

                      I was brought up to respect my elders. I don't respect many people nowadays.
                      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                      D 1 Reply Last reply
                      0
                      • D devvvy

                        why the appreciation? It's more code than a simple while loop with a Sleep. why you want be thankful when there really isn't a justification for it? M$ probably deprecate Thread.Sleep either because its syntax laywer told him so or simply it isn't "cool"

                        dev

                        G Offline
                        G Offline
                        GuyThiebaut
                        wrote on last edited by
                        #24

                        devvvy wrote:

                        why the appreciation? It's more code than a simple while loop with a Sleep.   why you want be thankful when there really isn't a justification for it?

                        Because someone, especially as it is a person I don't personally know, went out of their way to offer me help. In my books that deserves my appreciation ;)

                        “That which can be asserted without evidence, can be dismissed without evidence.”

                        ― Christopher Hitchens

                        D 1 Reply Last reply
                        0
                        • D devvvy

                          sure as i pointed out earlier timer/event subscriber - but why bother. THUS FAR there isn't ONE SINGLE argument that justify against use of Thread.Sleep against SCENARIO 2. I hereby invite you synatex lawyers/bitches to put forth your challenge to overthrow this hypothesis.

                          dev

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

                          devvvy wrote:

                          THUS FAR there isn't ONE SINGLE argument that justify against use of Thread.Sleep against SCENARIO 2.

                          Errm. Yes, we have posted arguments against, you're just choosing to ignore them. As for the reason I show Monitor.Wait is that it is close to the behaviour you're after with your Thread.Sleep, in that it puts the ThreadState into WaitSleepJoin, but it doesn't yield the thread to the OS. This is an important point because you can wake this thread up if you need to. This is the point you seem to be missing. Thread.Sleep - there is no wake up, other than through a Thread.Abort and we've already covered how that makes things unpredicable - if you need to cancel the thread and have it tidy itself up you have to wait for it to wake up. Also, Dave has a very valid point about the behaviour in STA COM. In this scenario, again, this is a justification that Thread.Sleep should not be used. It is not the appropriate mechanism.

                          I was brought up to respect my elders. I don't respect many people nowadays.
                          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                          N D 2 Replies Last reply
                          0
                          • D devvvy

                            still - there's nothing against SCENARIO 2, if it's not a UI thread or one with a message pump. Thread.Sleep isn't evil - it's just uncool. It's uncool because most developers tell each other it's uncool to do so.

                            dev

                            D Offline
                            D Offline
                            Dave Kreskowiak
                            wrote on last edited by
                            #26

                            OK, it's "uncool" because most people (noobs) use it like they use PictureBox. They use it without knowing it's limitations and without knowing that there are far better alternatives to it.

                            A guide to posting questions on CodeProject[^]
                            Dave Kreskowiak

                            D 1 Reply Last reply
                            0
                            • P Pete OHanlon

                              devvvy wrote:

                              THUS FAR there isn't ONE SINGLE argument that justify against use of Thread.Sleep against SCENARIO 2.

                              Errm. Yes, we have posted arguments against, you're just choosing to ignore them. As for the reason I show Monitor.Wait is that it is close to the behaviour you're after with your Thread.Sleep, in that it puts the ThreadState into WaitSleepJoin, but it doesn't yield the thread to the OS. This is an important point because you can wake this thread up if you need to. This is the point you seem to be missing. Thread.Sleep - there is no wake up, other than through a Thread.Abort and we've already covered how that makes things unpredicable - if you need to cancel the thread and have it tidy itself up you have to wait for it to wake up. Also, Dave has a very valid point about the behaviour in STA COM. In this scenario, again, this is a justification that Thread.Sleep should not be used. It is not the appropriate mechanism.

                              I was brought up to respect my elders. I don't respect many people nowadays.
                              CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                              N Offline
                              N Offline
                              N a v a n e e t h
                              wrote on last edited by
                              #27

                              Pete O'Hanlon wrote:

                              Errm. Yes, we have posted arguments against, you're just choosing to ignore them.

                              I second that. He don't have the maturity to understand what people are saying. So no point in discussing further. :)

                              Best wishes, Navaneeth My blog

                              D 1 Reply Last reply
                              0
                              • D devvvy

                                sure as i pointed out earlier timer/event subscriber - but why bother. THUS FAR there isn't ONE SINGLE argument that justify against use of Thread.Sleep against SCENARIO 2. I hereby invite you synatex lawyers/bitches to put forth your challenge to overthrow this hypothesis.

                                dev

                                N Offline
                                N Offline
                                N a v a n e e t h
                                wrote on last edited by
                                #28

                                devvvy wrote:

                                THUS FAR there isn't ONE SINGLE argument that justify against use of Thread.Sleep against SCENARIO 2.

                                As Pete already told, you are ignoring the comments that we made for Scenario2. See my answer.

                                Best wishes, Navaneeth My blog

                                D 1 Reply Last reply
                                0
                                • P Pete OHanlon

                                  devvvy wrote:

                                  THUS FAR there isn't ONE SINGLE argument that justify against use of Thread.Sleep against SCENARIO 2.

                                  Errm. Yes, we have posted arguments against, you're just choosing to ignore them. As for the reason I show Monitor.Wait is that it is close to the behaviour you're after with your Thread.Sleep, in that it puts the ThreadState into WaitSleepJoin, but it doesn't yield the thread to the OS. This is an important point because you can wake this thread up if you need to. This is the point you seem to be missing. Thread.Sleep - there is no wake up, other than through a Thread.Abort and we've already covered how that makes things unpredicable - if you need to cancel the thread and have it tidy itself up you have to wait for it to wake up. Also, Dave has a very valid point about the behaviour in STA COM. In this scenario, again, this is a justification that Thread.Sleep should not be used. It is not the appropriate mechanism.

                                  I was brought up to respect my elders. I don't respect many people nowadays.
                                  CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                                  D Offline
                                  D Offline
                                  devvvy
                                  wrote on last edited by
                                  #29

                                  1. As I pointed out earlier, there's no need to know *exact* when the blocked thread wakes up again. 2. STA COM debug messages -> how's debug messages in debug mode in a thread with a message pump affect me if all i've got is a non-ui baclground thread?

                                  dev

                                  P 1 Reply Last reply
                                  0
                                  • N N a v a n e e t h

                                    devvvy wrote:

                                    THUS FAR there isn't ONE SINGLE argument that justify against use of Thread.Sleep against SCENARIO 2.

                                    As Pete already told, you are ignoring the comments that we made for Scenario2. See my answer.

                                    Best wishes, Navaneeth My blog

                                    D Offline
                                    D Offline
                                    devvvy
                                    wrote on last edited by
                                    #30

                                    i pointed out timer + event handler as an alternative in the first place, but it's more lines of code for no additional benefit than a simple while+Sleep

                                    dev

                                    1 Reply Last reply
                                    0
                                    • D Dave Kreskowiak

                                      OK, it's "uncool" because most people (noobs) use it like they use PictureBox. They use it without knowing it's limitations and without knowing that there are far better alternatives to it.

                                      A guide to posting questions on CodeProject[^]
                                      Dave Kreskowiak

                                      D Offline
                                      D Offline
                                      devvvy
                                      wrote on last edited by
                                      #31

                                      it's a simple reliable picture box. if people use it for SCENARIO 1/3 it's just absurb in the first place. but SCENARIO 2 it's just not much to talk about - the fact there is simply because people wasting each other time trying to complicate otherwise very simple Thread.Sleep

                                      dev

                                      D 1 Reply Last reply
                                      0
                                      • G GuyThiebaut

                                        devvvy wrote:

                                        why the appreciation? It's more code than a simple while loop with a Sleep.   why you want be thankful when there really isn't a justification for it?

                                        Because someone, especially as it is a person I don't personally know, went out of their way to offer me help. In my books that deserves my appreciation ;)

                                        “That which can be asserted without evidence, can be dismissed without evidence.”

                                        ― Christopher Hitchens

                                        D Offline
                                        D Offline
                                        devvvy
                                        wrote on last edited by
                                        #32

                                        right, but do you see the point on why you need replace Thread.Sleep for SCENARIO 2 with ten more lines of code (re-implement with Timer+Event handler)? If no, your *thank you* will confuse other users for taking that really "Thread.Sleep" is evil, casting further confusion and un-necessary complication on this otherwise simple subject (You should thank those syntax lawyers for this). That's how we never get to bottom of things

                                        dev

                                        1 Reply Last reply
                                        0
                                        • P Pete OHanlon

                                          devvvy wrote:

                                          why the appreciation? It's more code than a simple while loop with a Sleep.

                                          So, you think he has to write this out every time? Chuck it in a library and you're done - a simple

                                          new Utility().WaitForTime(5000);

                                          I was brought up to respect my elders. I don't respect many people nowadays.
                                          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                                          D Offline
                                          D Offline
                                          devvvy
                                          wrote on last edited by
                                          #33

                                          Peter - this is nice/good idea. But this detracts the discussion to get to the bottom: SCENARIO 1/3 is just plain stupid to use Thread.Sleep - so just forget it. SCENARIO 2 - there's yet one single real justification to establish "Thread.Sleep" is *Evil* (as i pointed out in very beginning timer+event handler *can* be alternative - but why bother)

                                          dev

                                          P 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