挂起和恢复线程的替代方法有哪些?
自 .NET 2.0 以来,Thread.Suspend()
和 Thread.Resume()
这两个方法已过时。 为什么? 还有哪些其他替代方案和示例?
The two methods Thread.Suspend()
and Thread.Resume()
are obsolete since .NET 2.0. Why? What are other alternatives and any examples?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您将需要使用 AutoResetEvent EventWaitHandle。
假设您想做这样的事情(注意:不要这样做!):
就像其他人所说的那样,这是一个坏主意。 尽管仅在其自己的线程上使用 Suspend 相对安全,但您永远无法弄清楚当线程实际挂起时是否正在调用 Resume。 因此 Suspend 和 Resume 已被废弃。
相反,您希望使用 AutoResetEvent:
工作线程将等待等待句柄,直到另一个线程调用 StartWorking。 它的工作原理与挂起/恢复非常相似,因为 AutoResetEvent 只允许“恢复”一个线程。
You'll want to use an AutoResetEvent EventWaitHandle.
Say you want to do something like this (NOTE: don't do this!):
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. So Suspend and Resume have been obsoleted.
Instead, you want to use an AutoResetEvent:
The worker thread will wait on the wait handle until another thread calls StartWorking. It works much the same as Suspend/Resume, as the AutoResetEvent only allows one thread to be "resumed".
好的替代方案都是通过线程达到乐意等待的程度来工作的。 挂起是危险的,因为它可能会在线程持有互斥锁时挂起线程,这会导致死锁。
因此,您的线程需要的是一个它可以等待的 ManualResetEvent - 在它没有持有任何锁的时候,它可以安全地这样做。
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.
这是有史以来最好的 Thread 教程(针对 C#): http://www.albahari.com/threading/< /a>
对于等待,您需要在线程上使用 .Join() 。 这将等到胎面完成后进行。 否则,您将需要使用 Wait/Pulse。
This is the best tutorial ever for Thread (for C#): http://www.albahari.com/threading/
For wait you need to use .Join() on the thread. This will wait until tread finish is job. Other wise you will need to use Wait/Pulse.
您可以使用手动重置而不是自动重置:
you can use ManualReset instead of AutoReset:
那个太长了。 我需要的是一个可以使用的快速示例代码。 我从讨论中找到了一个答案,由 Mark R. Dawson 在 http:// bytes.com/groups/net-c/458947-thread-suspend。 它解释了过时方法的危险以及如何使用 AutoResetEvent 通知第二个线程继续处理。
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. It explains the danger of the obsolete methods and how to use AutoResetEvent to notify the second thread to continue processing.
Thread.Suspend()
和Thread.Resume()
在 .NET 中过时或删除的原因与Thread.suspend()< 的原因大致相同。 /code> 和
Thread.resume()
在 Java 中已过时。 比较 —挂起
“本质上容易发生死锁”),与The reasons why
Thread.Suspend()
andThread.Resume()
are obsolete or removed in .NET are largely the same reasons whyThread.suspend()
andThread.resume()
are obsolete in Java. Compare—suspend
"is inherently deadlock prone"), with解决方案:
让一个线程仅在另一个线程已挂起时才恢复另一个线程。
因此,第一个线程仅在另一个线程挂起自身(即其 ThreadState = Suspished)时才恢复另一个线程,从而做好恢复准备。 这看起来很安全& 完美无瑕。
或者,我不理解.Net 线程吗?
Solution:
Have a thread only resume another thread if the other thread has suspended itself.
Thus, the first thread only resumes the other thread if the other thread suspended itself (ie. its ThreadState = Suspended), and, thus, made itself ready to be resumed. This seems safe & flawless.
Or, am I not understanding .Net threading?
我同意这是一个很棒的教程。 Suspend() 和 Resume() 已过时的主要原因是它们是非常危险的方法。 在任何时候,Thread t 都可以做任何事情。 任何事物。 想象一下你的线程正在读取一个文件并且有一个锁。 你暂停你的线程。 文件保持锁定状态。 对于任何其他资源也是如此。 互斥锁上的锁也是如此。
I agree that is a great tutorial. The main reason Suspend() and Resume() are obsolete is because they are pretty dangerous methods. At any point Thread t could be doing anything. 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.