暂停线程 [英] Suspending Threads

查看:78
本文介绍了暂停线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个应用程序有一些工作线程,经常需要停止

并等待来自其他线程的更多信息。这些暂停将会花费很长时间(几分钟),并且我希望线程能够在新信息到达后尽快开始工作,因此线程。睡觉

不是最好的解决方案。在.NET 1.1中,我使用.Suspend和.Resume处理

这些暂停,但在.NET 2.0中,这些方法不再受到青睐。我有

一直在使用.Sleep(Infinite)和.Interupt而这似乎是

正常工作,但这是替换.Suspend和.Resume的最好方法?

有更好的选择吗?


Dave

Hi all,

I have an application which has some worker threads which often have to stop
and wait for some further information from other threads. These pauses will
often take a long time (a couple of minutes) and I want the thread to be
able to begin work ASAP once the new information arrives so Thread.Sleep
isn''t the best solution. In .NET 1.1 I used .Suspend and .Resume to handle
these pauses but in .NET 2.0 these methods are no longer favoured. I have
been using .Sleep(Infinite) and .Interupt instead and this seems to be
working okay, but is this the best way to replace .Suspend and .Resume? Are
there better alternatives available?

Dave

推荐答案

暂停是由于任意固有的问题而被删除

停止一个线程(例如,它可以在更新

a复杂不变量或线程有锁定时暂停线程)。


将线程置于等待状态的最佳方法是使用同步

对象。您所描述的内容听起来像是需要在某种类型的

事件中唤醒一个线程。一个WaitHandle派生的类列表ManualResetEvent可能适合这个账单。


我实际上写了一个使用事件的通用线程替换类

暂停和恢复。参见
http://msmvps.com/blogs/peterritchie...2E002E00_.aspx

-

浏览 http://connect.microsoft.com/VisualStudio/feedback/ 并投票。
http://www.peterRitchie.com/博客/

Microsoft MVP,Visual Developer - Visual C#

" Dave"写道:
Suspend was removed because of the inherent problems with arbitrarily
stopping a thread (e.g. it could suspend the thread in the middle of updating
a complex invariant or while the thread had a lock).

The best way to put a thread into a wait state is to use a synchronization
object. What you describe sounds like you need to awaken a thread on an
event of some sort. A WaitHandle-derived class list ManualResetEvent would
probably fit the bill.

I actually wrote a generalized Thread replacement class that uses an event
to suspend and resume. See
http://msmvps.com/blogs/peterritchie...2E002E00_.aspx
--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"Dave" wrote:

大家好,


我有一个应用程序有一些工作线程经常不得不停止
并等待来自其他线程的更多信息。这些暂停将会花费很长时间(几分钟),并且我希望线程能够在新信息到达后尽快开始工作,因此线程。睡觉

不是最好的解决方案。在.NET 1.1中,我使用.Suspend和.Resume处理

这些暂停,但在.NET 2.0中,这些方法不再受到青睐。我有

一直在使用.Sleep(Infinite)和.Interupt而这似乎是

正常工作,但这是替换.Suspend和.Resume的最好方法?

有更好的选择吗?


Dave
Hi all,

I have an application which has some worker threads which often have to stop
and wait for some further information from other threads. These pauses will
often take a long time (a couple of minutes) and I want the thread to be
able to begin work ASAP once the new information arrives so Thread.Sleep
isn''t the best solution. In .NET 1.1 I used .Suspend and .Resume to handle
these pauses but in .NET 2.0 these methods are no longer favoured. I have
been using .Sleep(Infinite) and .Interupt instead and this seems to be
working okay, but is this the best way to replace .Suspend and .Resume? Are
there better alternatives available?

Dave


看起来确实如此EventWaitHandle会做的伎俩但是有没有什么特别的原因我应该执行代码更改并使用额外的对象

当前代码似乎正在工作?我不介意,如果有一个好的

的原因,但如果它只是使用不同的方法似乎没有

多点。


Dave


" Peter Ritchie [C#MVP]" < PR **** @ newsgroups.nospamwrote in message

news:63 ************************** ******** @ microsof t.com ...
It does look like the EventWaitHandle will do the trick but is there any
particular reason I should perform the code changes and use an extra object
when the current code seems to be working? I don''t mind if there is a good
reason but if it''s just to use a different method there doesn''t seem to be
much point.

Dave

"Peter Ritchie [C# MVP]" <PR****@newsgroups.nospamwrote in message
news:63**********************************@microsof t.com...

由于任意固有问题,暂停被删除

停止一个线程(例如它可以暂停线程在

更新

a复杂不变量或线程锁定时)。


将线程置于等待状态的最佳方法是使用同步

对象。您所描述的内容听起来像是需要在某种类型的

事件中唤醒一个线程。一个WaitHandle派生的类列表ManualResetEvent



可能适合该账单。


我实际上写了一个通用的线程替换类,使用事件

暂停和恢复。参见
http://msmvps.com/blogs/peterritchie...2E002E00_.aspx

-

浏览 http://connect.microsoft.com/VisualStudio/feedback/ 并投票。
http://www.peterRitchie.com/博客/

Microsoft MVP,Visual Developer - Visual C#


" Dave"写道:
Suspend was removed because of the inherent problems with arbitrarily
stopping a thread (e.g. it could suspend the thread in the middle of
updating
a complex invariant or while the thread had a lock).

The best way to put a thread into a wait state is to use a synchronization
object. What you describe sounds like you need to awaken a thread on an
event of some sort. A WaitHandle-derived class list ManualResetEvent
would
probably fit the bill.

I actually wrote a generalized Thread replacement class that uses an event
to suspend and resume. See
http://msmvps.com/blogs/peterritchie...2E002E00_.aspx
--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"Dave" wrote:

>大家好,

我有一个应用程序,它有一些工作线程,经常需要停止
并等待来自其他线程的更多信息。这些暂停
通常需要很长时间(几分钟),我希望线程能够在新信息到达后尽快开始工作,因此Thread.Sleep
不是最好的解决方案。在.NET 1.1中,我使用.Suspend和.Resume来处理这些暂停,但在.NET 2.0中,这些方法不再受到青睐。我已经使用.Sleep(Infinite)和.Interupt代替,这似乎工作正常,但这是替换的最佳方式.Suspend和.Resume?

Dave
>Hi all,

I have an application which has some worker threads which often have to
stop
and wait for some further information from other threads. These pauses
will
often take a long time (a couple of minutes) and I want the thread to be
able to begin work ASAP once the new information arrives so Thread.Sleep
isn''t the best solution. In .NET 1.1 I used .Suspend and .Resume to
handle
these pauses but in .NET 2.0 these methods are no longer favoured. I have
been using .Sleep(Infinite) and .Interupt instead and this seems to be
working okay, but is this the best way to replace .Suspend and .Resume?
Are
there better alternatives available?

Dave



Dave< fa *** **** @ dodo.com.auwrote:
Dave <fa*******@dodo.com.auwrote:

它确实看起来像EventWaitHandle会做的但是有没有特别的

原因我应该执行代码更改并使用额外的对象

当前的代码似乎工作?我不介意,如果有一个好的

的原因,但如果它只是使用不同的方法似乎没有

多点。
It does look like the EventWaitHandle will do the trick but is there any
particular reason I should perform the code changes and use an extra object
when the current code seems to be working? I don''t mind if there is a good
reason but if it''s just to use a different method there doesn''t seem to be
much point.



似乎工作在线程中没有足够的保证。

当你不知道是什么锁定它时,任意暂停另一个线程

持有等是一个坏主意(就像流产一样)他们任意的是一个坏的

想法。


我个人喜欢Monitor.Wait / Pulse而不是EventWaitHandle,除非我需要
需要一些后者提供的额外功能,但两者都比暂停/恢复线程更好 - 这意味着

线程可以在*它*知道它时有效地暂停自己'

适当的点。


-

Jon Skeet - < sk *** @ pobox。 com>
http://www.pobox.com/~skeet博客: http://www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件

"Seems to be working" isn''t a good enough guarantee in threading.
Arbitrarily suspending another thread when you don''t know what locks it
holds etc is a bad idea (just like aborting them arbitrarily is a bad
idea).

I personally favour Monitor.Wait/Pulse over EventWaitHandle unless I
need some of the extra functionality the latter provides, but both are
certainly better than suspending/resuming the thread - it means the
thread can effectively pause itself when *it* knows it''s at an
appropriate point.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


这篇关于暂停线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆