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 / C++ / MFC
  4. Sleep

Sleep

Scheduled Pinned Locked Moved C / C++ / MFC
13 Posts 7 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.
  • V Offline
    V Offline
    VVVimal
    wrote on last edited by
    #1

    Hi I just tried to call Sleep(1000) in a thread function But when i tried to Unit threadFun(LPVOID lparam) { while(1) { Sleep(1000); // When tried to give break point over here appliacation gets terminates ............ } } Can any one give me solution to this Thanks

    D R E C C 5 Replies Last reply
    0
    • V VVVimal

      Hi I just tried to call Sleep(1000) in a thread function But when i tried to Unit threadFun(LPVOID lparam) { while(1) { Sleep(1000); // When tried to give break point over here appliacation gets terminates ............ } } Can any one give me solution to this Thanks

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      VVVimal wrote:

      Can any one give me solution to this

      Remove the call to Sleep(). It's almost always unnecessary and usually indicates a design flaw.

      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      V 1 Reply Last reply
      0
      • D David Crow

        VVVimal wrote:

        Can any one give me solution to this

        Remove the call to Sleep(). It's almost always unnecessary and usually indicates a design flaw.

        "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        V Offline
        V Offline
        VVVimal
        wrote on last edited by
        #3

        But i need a Sleep() function over there. Because i need a function to be get called every 1000 milliseconds

        D M 2 Replies Last reply
        0
        • V VVVimal

          But i need a Sleep() function over there. Because i need a function to be get called every 1000 milliseconds

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          VVVimal wrote:

          Because i need a function to be get called every 1000 milliseconds

          Then use SetTimer(1000).

          "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          1 Reply Last reply
          0
          • V VVVimal

            Hi I just tried to call Sleep(1000) in a thread function But when i tried to Unit threadFun(LPVOID lparam) { while(1) { Sleep(1000); // When tried to give break point over here appliacation gets terminates ............ } } Can any one give me solution to this Thanks

            R Offline
            R Offline
            Rajesh R Subramanian
            wrote on last edited by
            #5

            VVVimal wrote:

            Unit threadFun(LPVOID lparam) { while(1) { Sleep(1000); // When tried to give break point over here appliacation gets terminates ............ } }

            I don't see why should the application "terminate" with this piece of code. (It might have probably helped us if you had given the remaining code within the thread function to see if anything is wrong) With more than one thread running, may be the issue is somewhere else? Besides that, like David said, remove the unnecessary Sleep call and replace it with something that makes sense of some sort.

            It is a crappy thing, but it's life -^ Carlo Pallini

            E 1 Reply Last reply
            0
            • V VVVimal

              But i need a Sleep() function over there. Because i need a function to be get called every 1000 milliseconds

              M Offline
              M Offline
              Maximilien
              wrote on last edited by
              #6

              Remember that Windows is not a real-time system, your function might or might not be called at every 1000 milliseconds, depending on what's happening on the OS.

              This signature was proudly tested on animals.

              1 Reply Last reply
              0
              • R Rajesh R Subramanian

                VVVimal wrote:

                Unit threadFun(LPVOID lparam) { while(1) { Sleep(1000); // When tried to give break point over here appliacation gets terminates ............ } }

                I don't see why should the application "terminate" with this piece of code. (It might have probably helped us if you had given the remaining code within the thread function to see if anything is wrong) With more than one thread running, may be the issue is somewhere else? Besides that, like David said, remove the unnecessary Sleep call and replace it with something that makes sense of some sort.

                It is a crappy thing, but it's life -^ Carlo Pallini

                E Offline
                E Offline
                etkid84
                wrote on last edited by
                #7

                does the application terminate or does it hang? i would thing that statement would cause the app to hang... continuous loop with a defined wait time.. is that 1000 seconds or milliseconds it's library specific..??

                David

                R 1 Reply Last reply
                0
                • E etkid84

                  does the application terminate or does it hang? i would thing that statement would cause the app to hang... continuous loop with a defined wait time.. is that 1000 seconds or milliseconds it's library specific..??

                  David

                  R Offline
                  R Offline
                  Rajesh R Subramanian
                  wrote on last edited by
                  #8

                  David, you again replied to me instead of the OP. You should click reply on the specific message that you want to reply to.

                  It is a crappy thing, but it's life -^ Carlo Pallini

                  1 Reply Last reply
                  0
                  • V VVVimal

                    Hi I just tried to call Sleep(1000) in a thread function But when i tried to Unit threadFun(LPVOID lparam) { while(1) { Sleep(1000); // When tried to give break point over here appliacation gets terminates ............ } } Can any one give me solution to this Thanks

                    E Offline
                    E Offline
                    etkid84
                    wrote on last edited by
                    #9

                    my questions are buried below..

                    David

                    1 Reply Last reply
                    0
                    • V VVVimal

                      Hi I just tried to call Sleep(1000) in a thread function But when i tried to Unit threadFun(LPVOID lparam) { while(1) { Sleep(1000); // When tried to give break point over here appliacation gets terminates ............ } } Can any one give me solution to this Thanks

                      C Offline
                      C Offline
                      Chuck OToole
                      wrote on last edited by
                      #10

                      I use Sleep() all the time in threaded code, there's no reason it should cause the termination in and of itself. However, the thread code you show runs in the context of a larger program, notably the main thread which created the thread you show. What is that thread doing? If it exits, it will take the created thread down with it. Make sure the main code is waiting for this thread to terminate before it does.

                      1 Reply Last reply
                      0
                      • V VVVimal

                        Hi I just tried to call Sleep(1000) in a thread function But when i tried to Unit threadFun(LPVOID lparam) { while(1) { Sleep(1000); // When tried to give break point over here appliacation gets terminates ............ } } Can any one give me solution to this Thanks

                        C Offline
                        C Offline
                        chandu004
                        wrote on last edited by
                        #11

                        VVVimal wrote:

                        When tried to give break point over here appliacation gets terminates

                        do you mean, it is getting terminated when you try to debug?

                        -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

                        V 1 Reply Last reply
                        0
                        • C chandu004

                          VVVimal wrote:

                          When tried to give break point over here appliacation gets terminates

                          do you mean, it is getting terminated when you try to debug?

                          -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

                          V Offline
                          V Offline
                          VVVimal
                          wrote on last edited by
                          #12

                          I just added WatiForSingleObject(threadHandle,INFINITE) now working fine. Can any one tell why does it so

                          C 1 Reply Last reply
                          0
                          • V VVVimal

                            I just added WatiForSingleObject(threadHandle,INFINITE) now working fine. Can any one tell why does it so

                            C Offline
                            C Offline
                            Chuck OToole
                            wrote on last edited by
                            #13

                            My answer above told you exactly why WaitForSingleObject() solved the problem However, the thread code you show runs in the context of a larger program, notably the main thread which created the thread you show. What is that thread doing? If it exits, it will take the created thread down with it.

                            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