none
How to pause a thread RRS feed

  • Question

  • Since Thread.Suspend() has been deprecated I'm wondering what I should use to pause a thread ? I'm not intrested in synchronizing threads. I have a worker thread that is completely separated from other threads, no shared resources or data, and I want to pause it to save CPU usage. The worker thread is quite complex so implementing pause with a AutoResetEvent or a Semaphore would mean that the thread would have to have many synchronazation "blocks" in order to respond quickly to a pause event.

     

    I know about potential problems that Thread.Suspend() and Thread.Resume() might cause but since I have no shared resources they don't concern me ?

     

    For example: Main application starts a worker thread to compresses a memorystream. How would I do to quickly pause this worker thread ?

     

    Code Snippet

    public void MainApplicationThread()

    {

      Thread trWork;

     

     

      trWork = new Thread( WorkerThread );

      trWork.Start( null ); // only null in example

    }

     

    public void WorkerThread( MemoryStream ms )

    {

      DoSomeWork1();

      DoSomeWork2();

      DoSomeWork3();

      //...you get the idea

      DoSomeWork1000();

    }

     

    Tuesday, May 6, 2008 12:35 PM

Answers

All replies

  • You're on the wrong "thread" with this.  What's going to happen when you resume the thread?  Is it going to do something differently?  How does it know that it needs to do something differently if though you stopped it right in the middle of DoSomeWork99()?  "Synchronization object" is the inevitable answer.

    Consider ThreadPool.QueueUserWorkItem to split up the job in small packets of work.
    Wednesday, May 7, 2008 12:58 AM
    Moderator
  • No it is not going to do somthing differently when it resumes work. The soule purpuse of pausing the thread is to save CPU processing for the moment. It should resume like nothing happned. If it is completely impossible to pause and resume then it would probably be better for me to tamper with the thread priority.

    Wednesday, May 7, 2008 6:38 AM
  • Yeah, thread priority is okay.  Although I doubt you'll see much difference.  Dual-core CPUs have been around quite a while now.  And Windows always gives the interactive thread more cycles if needed.
    Wednesday, May 7, 2008 9:41 AM
    Moderator
  •  nobugz wrote:
    Yeah, thread priority is okay.  Although I doubt you'll see much difference.  Dual-core CPUs have been around quite a while now.  And Windows always gives the interactive thread more cycles if needed.

     

    It's more of a problem if you have multiple worker threads. If I set 1 of the threads to lowest priority it still gets some CPU time. I don't understand why Thread.Suspend() and Thread.Resume() has been deprecated. Have they been deprecated just because most people use them incorrectly/dangerously ?

    Wednesday, May 7, 2008 10:04 AM
  • Yes.  It was universally misused to synchronize threads.
    Wednesday, May 7, 2008 11:04 AM
    Moderator