How to pause / resume a thread in C#

Posted by Anuraj on Sunday, June 16, 2013

.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.


Similar Posts


Did you like this article? Share it with your friends

Facebook Twitter Google+ LinkedIn Reddit StumbleUpon



MVP
Subscribe
Archives


Copyright © 2017 - Anuraj P. 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. Hosted with ❤ by GitHub