恢复 C# 线程
可能重复的问题: 有没有办法无限期地暂停线程?
在我的代码中,我执行了以下操作
Thread mainThread
//...
mainThread.Resume();
void StartThread()
{
while (!wantQuit)
{
db.runEmail();
mainThread.Suspend();
}
}
然后我得到了下面的异常,因为我在它尚未暂停时调用了resume。
System.Threading.ThreadStateException
我注意到这个警告
warning CS0618: 'System.Threading.Thread.Resume()' is obsolete: 'Thread.Resume has been deprecated. Please use other classes in System.Threading, such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect resources. http://go.microsoft.com/fwlink/?linkid=14202'
所以我想知道有没有办法恢复并忽略它没有暂停的异常/情况?我讨厌写下面的内容而不是只写一行
try
{
mainThread.Resume();
}
catch (System.Threading.ThreadStateException e)
{
}
Possible duplicate question: Is there a way to indefinitely pause a thread?
In my code i do the below
Thread mainThread
//...
mainThread.Resume();
void StartThread()
{
while (!wantQuit)
{
db.runEmail();
mainThread.Suspend();
}
}
Then i get the exception below because i call resume when it hasnt been suspended.
System.Threading.ThreadStateException
I notice this warning
warning CS0618: 'System.Threading.Thread.Resume()' is obsolete: 'Thread.Resume has been deprecated. Please use other classes in System.Threading, such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect resources. http://go.microsoft.com/fwlink/?linkid=14202'
So i wonder is there a way to resume and ignore the exception/case that it is not suspended? I hate writing the below instead of just one line
try
{
mainThread.Resume();
}
catch (System.Threading.ThreadStateException e)
{
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
简单的解决方案是修复您的逻辑,并且在未 Suspending() 时不要调用 Resume()。
但 Resume/Suspend API 确实已弃用,请看一下,例如:
1) 监视器、Wait() 和 Pulse()
2) AutoResetEvent 或 ManualResetEvent、Set() 和 WaitOne()
静态类 Monitor 更容易使用,并且与
lock() {}
集成,但是ResetEvent 在这里可能更合适,因为您可以在设置或取消设置模式下创建它,并且“错过”信号的可能性较小。相比之下,当稍后发生 Wait() 时,Monitor.Pulse() 就会丢失。将在您的问题中插入一个链接,虽然我不认为它是重复的,接受的答案非常适合治愈您使用挂起/恢复的问题。
The easy solution is to fix your logic and don't call Resume() when you are not Suspended().
But the Resume/Suspend API is indeed deprecated, take a look at, for example:
1) Monitor, Wait() and Pulse()
2) AutoResetEvent or ManualResetEvent, Set() and WaitOne()
The static class Monitor is a little easier to use and integrates with
lock() {}
, but a ResetEvent might be more appropriate here because you can create it in set or unset mode and you are less likely to 'miss' a signal. By contrast, a Monitor.Pulse() will just be lost when the Wait() happens later.Will inserted a link in your question, and while I don't consider it a duplicate, the accepted answer there is well suited to cure you from using Suspend/Resume.
WaitHandles 是一种更好的方法。
不要挂起线程,而是让它在等待句柄上等待。当您准备再次启动时,您可以从另一个线程“设置”句柄。
有关代码示例,请参阅 MSDN 的 ManualResetEvent 文档 。
WaitHandles are a better approach here.
Instead of suspending your thread, have it wait on a waithandle. When you are ready to start up again, you "set" the handle from the other thread.
For a code example, see MSDN's documentation of ManualResetEvent.
在这种情况下,您可能需要考虑使用线程监视器。
You might want to take a look at using a Thread Monitor in this case.
您可以检查 Thread.ThreadState 并检查 ThreadState.Suspished 恢复之前。
You can check Thread.ThreadState and check for ThreadState.Suspended before resuming.