Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 64139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:44:35+00:00 2026-05-10T18:44:35+00:00

I’ve been working on a web crawling .NET app in my free time, and

  • 0

I’ve been working on a web crawling .NET app in my free time, and one of the features of this app that I wanted to included was a pause button to pause a specific thread.

I’m relatively new to multi-threading and I haven’t been able to figure out a way to pause a thread indefinitely that is currently supported. I can’t remember the exact class/method, but I know there is a way to do this but it has been flagged as obsolete by the .NET framework.

Is there any good general purpose way to indefinitely pause a worker thread in C# .NET.

I haven’t had a lot of time lately to work on this app and the last time I touched it was in the .NET 2.0 framework. I’m open to any new features (if any) that exist in the .NET 3.5 framework, but I’d like to know of solution that also works in the 2.0 framework since that’s what I use at work and it would be good to know just in case.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T18:44:35+00:00Added an answer on May 10, 2026 at 6:44 pm

    Never, ever use Thread.Suspend. The major problem with it is that 99% of the time you can’t know what that thread is doing when you suspend it. If that thread holds a lock, you make it easier to get into a deadlock situation, etc. Keep in mind that code you are calling may be acquiring/releasing locks behind the scenes. Win32 has a similar API: SuspendThread and ResumeThread. The following docs for SuspendThread give a nice summary of the dangers of the API:

    http://msdn.microsoft.com/en-us/library/ms686345(VS.85).aspx

    This function is primarily designed for use by debuggers. It is not intended to be used for thread synchronization. Calling SuspendThread on a thread that owns a synchronization object, such as a mutex or critical section, can lead to a deadlock if the calling thread tries to obtain a synchronization object owned by a suspended thread. To avoid this situation, a thread within an application that is not a debugger should signal the other thread to suspend itself. The target thread must be designed to watch for this signal and respond appropriately.

    The proper way to suspend a thread indefinitely is to use a ManualResetEvent. The thread is most likely looping, performing some work. The easiest way to suspend the thread is to have the thread ‘check’ the event each iteration, like so:

    while (true) {     _suspendEvent.WaitOne(Timeout.Infinite);      // Do some work... } 

    You specify an infinite timeout so when the event is not signaled, the thread will block indefinitely, until the event is signaled at which point the thread will resume where it left off.

    You would create the event like so:

    ManualResetEvent _suspendEvent = new ManualResetEvent(true); 

    The true parameter tells the event to start out in the signaled state.

    When you want to pause the thread, you do the following:

    _suspendEvent.Reset(); 

    And to resume the thread:

    _suspendEvent.Set(); 

    You can use a similar mechanism to signal the thread to exit and wait on both events, detecting which event was signaled.

    Just for fun I’ll provide a complete example:

    public class Worker {     ManualResetEvent _shutdownEvent = new ManualResetEvent(false);     ManualResetEvent _pauseEvent = new ManualResetEvent(true);     Thread _thread;      public Worker() { }      public void Start()     {         _thread = new Thread(DoWork);         _thread.Start();     }      public void Pause()     {         _pauseEvent.Reset();     }      public void Resume()     {         _pauseEvent.Set();     }      public void Stop()     {         // Signal the shutdown event         _shutdownEvent.Set();          // Make sure to resume any paused threads         _pauseEvent.Set();          // Wait for the thread to exit         _thread.Join();     }      public void DoWork()     {         while (true)         {             _pauseEvent.WaitOne(Timeout.Infinite);              if (_shutdownEvent.WaitOne(0))                 break;              // Do the work here..         }     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.