简体   繁体   English

什么是暂停和恢复线程的替代方法?

[英]What are alternative ways to suspend and resume a thread?

The two methods Thread.Suspend() and Thread.Resume() are obsolete since .NET 2.0. 自.NET 2.0以来, Thread.Suspend()Thread.Resume()这两个方法已经过时了。 Why? 为什么? What are other alternatives and any examples? 什么是其他替代品和任何例子?

You'll want to use an AutoResetEvent EventWaitHandle. 您将要使用AutoResetEvent EventWaitHandle。

Say you want to do something like this ( NOTE : don't do this!): 假设你想做这样的事情( 注意 :不要这样做!):

private Thread myThread;

private void WorkerThread() 
{
    myThread = Thread.CurrentThread;
    while (true)
    {
        myThread.Suspend();
        //Do work.
    }
}

public void StartWorking() 
{
    myThread.Resume();
}

Like others have said, this is a bad idea. 像其他人所说,这是一个坏主意。 Even though only using Suspend on its own thread is relatively safe, you can never figure out if you're calling Resume when the thread is actually suspended. 尽管只在自己的线程上使用Suspend是相对安全的,但是当线程实际被挂起时,你永远无法弄清楚你是否正在调用Resume。 So Suspend and Resume have been obsoleted. 所以Suspend and Resume已经过时了。

Instead, you want to use an AutoResetEvent: 相反,您想要使用AutoResetEvent:

private EventWaitHandle wh = new AutoResetEvent();

private void WorkerThread() 
{
    while(true) 
    {
        wh.WaitOne();
        //Do work.
    }
}

public void StartWorking()
{
    wh.Set();
}

The worker thread will wait on the wait handle until another thread calls StartWorking. 工作线程将等待等待句柄,直到另一个线程调用StartWorking。 It works much the same as Suspend/Resume, as the AutoResetEvent only allows one thread to be "resumed". 它与Suspend / Resume的工作原理大致相同,因为AutoResetEvent只允许一个线程“恢复”。

The good alternatives all work by the thread reaching a point where it is happy to wait. 好的选择都可以通过线程达到令人高兴等待的程度。 Suspend was dangerous because it could suspend the thread while it was holding a lock on a mutex - a recipe for deadlocks. 暂停是危险的,因为它可以在线程锁定互斥锁时挂起线程 - 这是一个死锁的秘诀。

So what your thread needs is a ManualResetEvent that it can Wait on - at a time when it is safe for it to do so, when it is not holding any locks. 所以你的线程需要的是一个可以等待的ManualResetEvent--当它没有持有任何锁时,它可以安全地执行。

This is the best tutorial ever for Thread (for C#): http://www.albahari.com/threading/ 这是Thread(C#)的最佳教程: http//www.albahari.com/threading/

For wait you need to use .Join() on the thread. 等待你需要在线程上使用.Join()。 This will wait until tread finish is job. 这将等到胎面完成工作。 Other wise you will need to use Wait/Pulse . 另外,你需要使用Wait / Pulse

you can use ManualReset instead of AutoReset: 您可以使用ManualReset而不是AutoReset:

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();
 Console.WriteLine("Thread started running");
 }

public void Pause()
 {
 _pauseEvent.Reset();
 Console.WriteLine("Thread paused");
 }

public void Resume()
 {
 _pauseEvent.Set();
 Console.WriteLine("Thread resuming ");
 }

public void Stop()
 {
 // Signal the shutdown event
 _shutdownEvent.Set();
 Console.WriteLine("Thread Stopped ");

// 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..
 Console.WriteLine("Thread is running");

 }
 }
}

That one is too long. 那个太长了。 What I need is a quick example codes to use. 我需要的是一个快速的示例代码。 I found one from the discussion and answered by Mark R. Dawson at http://bytes.com/groups/net-c/458947-thread-suspend . 我在讨论中找到了一个,并由Mark R. Dawson在http://bytes.com/groups/net-c/458947-thread-suspend回答。 It explains the danger of the obsolete methods and how to use AutoResetEvent to notify the second thread to continue processing. 它解释了过时方法的危险以及如何使用AutoResetEvent通知第二个线程继续处理。

I agree that is a great tutorial. 我同意这是一个很棒的教程。 The main reason Suspend() and Resume() are obsolete is because they are pretty dangerous methods. Suspend()和Resume()过时的主要原因是因为它们是非常危险的方法。 At any point Thread t could be doing anything. 在任何时候,线程t都可以做任何事情。 Anything. 任何东西。 Imagine your thread is reading a file and has a lock on it. 想象一下,你的线程正在读取文件并对其进行锁定。 You suspend your thread. 你暂停你的线程。 File stays locked. 文件保持锁定状态 Same goes for any other resources. 任何其他资源也是如此。 Same goes for a lock on a mutex. 锁定互斥锁也是如此。

The reasons why Thread.Suspend() and Thread.Resume() are obsolete or removed in .NET are largely the same reasons why Thread.suspend() and Thread.resume() are obsolete in Java. Thread.Suspend()Thread.Resume()在.NET中过时或删除的原因与在Java中过时使用Thread.suspend()Thread.resume()原因大致相同。 Compare— 相比-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM