How to pause / resume a thread in C#

June 16, 2013 by Anuraj

.Net ASP.Net Windows Forms

Here is the code snippet which will help you to pause / resume a thread using ManualResetEvent class. Both Suspend() and Resume() methods are deprecated in .Net Framework. So both of these methods not recommended to use.

private ManualResetEvent _manualResetEvent = new ManualResetEvent(true);

var thread = new Thread(() =>
{
    while (true)
    {
        //Do the work here
        _manualResetEvent.WaitOne(Timeout.Infinite);
    }
});

And to pause the thread, you can use

_manualResetEvent.Reset();

And to resume you can use

_manualResetEvent.Set();

You can find more details about ManualResetEvent in MSDN

Happy Programming.

Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub