Thread.Resume已经过时了吗?我们现在应该如何唤醒SleepingThreads? [英] Thread.Resume is Obsolete? How are we supposed to wake up SleepingThreads now?

查看:179
本文介绍了Thread.Resume已经过时了吗?我们现在应该如何唤醒SleepingThreads?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个线程,一旦它运行了一个

方法就会睡5分钟,然后如果它应该(bool = true)则重复它自己。

在2.0之前我能够在将bool标记为

false之后恢复该线程,这将导致线程完成它正在做的事情并且

完成,或者为它的睡眠状态醒来而不再循环回到

本身。


我们怎么告诉线程现在停止睡觉那个Thread.Resume已经过时了?

I have a thread that sleeps for 5 minutes once it''s finished running a
method and then it repeats itself if it''s supposed to (bool = true).
Prior to 2.0 I was able to resume the thread after marking the bool to
false which would cause the thread to finish what it was doing and
complete, or to wake up for it''s sleep state and not loop back into
itself anymore.

How do we tell a thread to stop sleeping now that Thread.Resume is obsolete?

推荐答案

Benny Raymond写道:
Benny Raymond wrote:
我有一个线程,睡了5分钟一次它已经完成了运行
方法然后它重复自己,如果它应该(bool = true)。
在2.0之前,我能够在将bool标记为<之后恢复该线程br /> false会导致线程完成它正在做的事情并完成,或者唤醒它的睡眠状态e并且不再循环回到
本身。

如果Thread.Resume已经过时,我们怎么告诉线程停止睡眠?
I have a thread that sleeps for 5 minutes once it''s finished running a
method and then it repeats itself if it''s supposed to (bool = true).
Prior to 2.0 I was able to resume the thread after marking the bool to
false which would cause the thread to finish what it was doing and
complete, or to wake up for it''s sleep state and not loop back into
itself anymore.

How do we tell a thread to stop sleeping now that Thread.Resume is obsolete?




你使用Monitor.Wait / Pulse或WaitHandles,这是你应该在b $ b之前做过的事情:)


参见 http://www.pobox.com/~skeet/csharp/t。 ..eadlocks.shtml (一半

下来)获取此类样本。


Jon



You use Monitor.Wait/Pulse or WaitHandles, which is what you should
have been doing before :)

See http://www.pobox.com/~skeet/csharp/t...eadlocks.shtml (half
way down) for a sample of this.

Jon


也不要使用Thread :: Suspend()。
http://joehacker.blogspot.com/2005/1...nd-method.html


Ab。
http://joehacker.blogspot.com


" Benny Raymond" <是*** @ pocketrocks.com>在留言中写道

新闻:O2 **************** @ TK2MSFTNGP14.phx.gbl ...
Also dont use Thread::Suspend().
http://joehacker.blogspot.com/2005/1...nd-method.html

Ab.
http://joehacker.blogspot.com

"Benny Raymond" <be***@pocketrocks.com> wrote in message
news:O2****************@TK2MSFTNGP14.phx.gbl...
我有一个一旦完成运行
方法,它会休眠5分钟,然后如果它应该(bool = true)它会重复。
在2.0之前,我能够恢复标记bool后的线程为false,这将导致线程完成它正在做的事情并完成,或唤醒它的睡眠状态而不是循环回到
本身我们怎么告诉线程停止睡眠,因为Thread.Resume是
I have a thread that sleeps for 5 minutes once it''s finished running a
method and then it repeats itself if it''s supposed to (bool = true).
Prior to 2.0 I was able to resume the thread after marking the bool to
false which would cause the thread to finish what it was doing and
complete, or to wake up for it''s sleep state and not loop back into
itself anymore.

How do we tell a thread to stop sleeping now that Thread.Resume is



已经过时了?


obsolete?


那很好,不过你给我看的东西还在睡觉 - 我需要

才能唤醒睡眠线程,或者终止它(就是那个时候) >
abort发挥作用?)


基本上我需要做的是:


* ClientA需要运行每5分钟

*如果SettingX c hanges为false,它允许ClientA完成运行

(如果它当前正在运行)但它将不再运行。

*如果SettingX变为true,它告诉ClientA再次开始备份

再过5分钟。

*如果App关闭,它允许ClientA完成运行(如果它是'

目前正在运行)但它将不再运行了。


之前我做过的事情(我可以​​看出为什么它现在错了但不是怎样

才能正确完成):

* ClassX创建ClientA类

* ClassX将ClientA中的loop_bool设置为true

* ClassX启动ClientA线程

* ClientA线程运行并在最后休眠

*当ClientA完成睡眠时,它循环,检查loop_bool和

如果loop_bool为真,则继续运行。

* ClassX中的SettingX更改

* ClassX将ClientA中的loop_bool设置为false

* ClassX简历ClientA线程

* ClassX J oins ClientA

* ClientA完成并且没有循环

* ClassX检查设置是否正确,如果是这样,它启动了

ClientA线程再次

请帮帮我..哈哈:)


~Benny

Jon Skeet [C#MVP ]写道:
That''s fine, however the thing you showed me is still sleeping - I need
to be able to wake up the sleeping thread, or terminate it (is that when
abort comes into play?)

Basically what I need to do is this:

* ClientA needs to be run every 5 minutes
* If SettingX changes to false, it allows ClientA to finish running
(if it''s currently running) but it will no longer run again.
* If SettingX changes to true, it tells ClientA to start back up on
it''s 5 minute cycle again.
* If App closes, it allows ClientA to finish running (if it''s
currently running) but it will no longer run again.

What I was doing before (which I can see why it''s wrong now but not how
to do it correctly):
* ClassX creates ClientA class
* ClassX sets loop_bool in ClientA to true
* ClassX starts ClientA thread
* ClientA thread runs and sleeps at the end
* When ClientA is done sleeping it loops, checks the loop_bool and
continus running if loop_bool is true.
* SettingX in ClassX changes
* ClassX sets loop_bool in ClientA to false
* ClassX Resumes ClientA thread
* ClassX Joins ClientA
* ClientA finishes and doesn''t loop
* ClassX checks setting to see if it''s true, if so it starts up the
ClientA thread again
Please help me.. haha :)

~Benny
Jon Skeet [C# MVP] wrote:
Benny Raymond写道:
Benny Raymond wrote:
我有一个线程,一旦完成运行
方法就会睡5分钟然后它重复自己,如果它应该(bool = true)。
在2.0之前,我能够在将bool标记为
false之后恢复该线程,这将导致线程完成它正在做和完成,或者为了它的睡眠状态醒来而不再回到
本身。

我们怎么告诉一个线程现在停止睡觉那个Thread.Resume已经过时了吗?
I have a thread that sleeps for 5 minutes once it''s finished running a
method and then it repeats itself if it''s supposed to (bool = true).
Prior to 2.0 I was able to resume the thread after marking the bool to
false which would cause the thread to finish what it was doing and
complete, or to wake up for it''s sleep state and not loop back into
itself anymore.

How do we tell a thread to stop sleeping now that Thread.Resume is obsolete?



你使用Monitor.Wait / Pulse或WaitHandles,这是你之前应该做的事情:)

http://www.pobox.com/~skeet/ csharp / t ... eadlocks.shtml (一半下来)获取此类样本。

Jon


You use Monitor.Wait/Pulse or WaitHandles, which is what you should
have been doing before :)

See http://www.pobox.com/~skeet/csharp/t...eadlocks.shtml (half
way down) for a sample of this.

Jon



这篇关于Thread.Resume已经过时了吗?我们现在应该如何唤醒SleepingThreads?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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