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. Resuming a sleeping thread

Resuming a sleeping thread

Scheduled Pinned Locked Moved C#
tutorialquestioncareer
7 Posts 5 Posters 2 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.
  • P Offline
    P Offline
    PeteConc
    wrote on last edited by
    #1

    Hi. I have created a thread that calls Thread.Sleep(60000) after doing some work, and then it does some more work. What I wanted to do is to make the thread to resume processing and not having to wait the 60000ms until the sleep ends. For example, a function that tests if the thread is asleep, and if so, wakes it up. Is there a way to do this? I tried with the 'Resume' function, but this works only if the thread is in 'suspended' state. Thanks.

    A S 2 Replies Last reply
    0
    • P PeteConc

      Hi. I have created a thread that calls Thread.Sleep(60000) after doing some work, and then it does some more work. What I wanted to do is to make the thread to resume processing and not having to wait the 60000ms until the sleep ends. For example, a function that tests if the thread is asleep, and if so, wakes it up. Is there a way to do this? I tried with the 'Resume' function, but this works only if the thread is in 'suspended' state. Thanks.

      A Offline
      A Offline
      alexey N
      wrote on last edited by
      #2

      Try to "Suspend" thread insted of "Sleep". My english is not so good. Please, correct my errors. Best regards, Alexey.

      P 1 Reply Last reply
      0
      • A alexey N

        Try to "Suspend" thread insted of "Sleep". My english is not so good. Please, correct my errors. Best regards, Alexey.

        P Offline
        P Offline
        PeteConc
        wrote on last edited by
        #3

        Thanks for the advice, but Suspend just suspends the thread infinitely. The normal behaviour of the thread should be: DoSomeWork Thread.Sleep(60000) DoSomeMoreWork But in certain occasions I need to tell the thread to execute 'DoSomeMoreWork' before the sleep ends, that is, tell it to stop sleeping and continue execution.

        L A 2 Replies Last reply
        0
        • P PeteConc

          Thanks for the advice, but Suspend just suspends the thread infinitely. The normal behaviour of the thread should be: DoSomeWork Thread.Sleep(60000) DoSomeMoreWork But in certain occasions I need to tell the thread to execute 'DoSomeMoreWork' before the sleep ends, that is, tell it to stop sleeping and continue execution.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Just put it in another thread instead of in main thread. Use some flag, for example "isFinished" or something like that. DoSomeWork while(isFinished == false) { ... waiting... } DoSomeMoreWork

          1 Reply Last reply
          0
          • P PeteConc

            Thanks for the advice, but Suspend just suspends the thread infinitely. The normal behaviour of the thread should be: DoSomeWork Thread.Sleep(60000) DoSomeMoreWork But in certain occasions I need to tell the thread to execute 'DoSomeMoreWork' before the sleep ends, that is, tell it to stop sleeping and continue execution.

            A Offline
            A Offline
            alexey N
            wrote on last edited by
            #5

            Use timer to manage this situation. My english is not so good. Please, correct my errors. Best regards, Alexey.

            V 1 Reply Last reply
            0
            • A alexey N

              Use timer to manage this situation. My english is not so good. Please, correct my errors. Best regards, Alexey.

              V Offline
              V Offline
              Vivek Sivasamy
              wrote on last edited by
              #6

              Use .net synchronisation mechanism . If u want ur worker thread to wait for a specific event use wait handles for the same. It is recomended not to use sleep to get the necessary syncronisation.

              1 Reply Last reply
              0
              • P PeteConc

                Hi. I have created a thread that calls Thread.Sleep(60000) after doing some work, and then it does some more work. What I wanted to do is to make the thread to resume processing and not having to wait the 60000ms until the sleep ends. For example, a function that tests if the thread is asleep, and if so, wakes it up. Is there a way to do this? I tried with the 'Resume' function, but this works only if the thread is in 'suspended' state. Thanks.

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

                Use one of the many synchronization objects that .NET provides. Take a look at AutoResetEvent[^], ManualResetEvent[^] and Mutex[^]. In all the cases, you would have to do something like

                Thread A

                DoX();
                syncObject.WaitOne(); // Wait until Thread B signals
                DoY();

                Thread B

                DoP();
                syncObject.Release(); // Signal Thread A to resume operations.
                DoQ();

                The advantage of using synchronization objects instead of looping with a flag is that threads waiting on a synchronization primitive do not consume CPU cycles. Windows knows about synchronization primitives and so doesn't schedule the thread for execution until the sync object is signalled. With looping, Windows is forced to schedule that thread and it uses up its time slice looping.

                Regards Senthil _____________________________

                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