打破线程睡眠 [英] Break thread sleep

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

问题描述

Hello group。


我有线程,我在循环中执行特定任务。我可以通过复选框指定

,在什么时间段完成任务,ex。

立即,5分钟休息,15分钟休息等等。现在我有它

像这样完成。


while(true)

{

period = checkIfPeriodChanged();

...

performTask

...

睡眠(期间);

}


这样可行,直到我想改变时间段。例如,当

我将周期设置为15分钟,并且在执行任务后我再次将其更改为

1分钟,我必须等待所有~15分钟,才能设置我的新设置( 1分钟进入

操作。有没有办法,打破睡眠(期间),或者我应该以非常不同的方式做到这一点

?我查看了System.Threading.Timer,但我不是。

确定它是否会正常工作,因为我不知道执行了多长时间

performTask将采取(差异可以相当广泛 - 从几个

秒到几分钟)。


我会感激任何帮助。

Masanobu Suga

Hello group.

I have thread in which I perform specific task in loop. I can specify,
by check box, in what periods of time that task should be done, ex.
instantly, with 5min break, with 15min break etc. For now I had it
done in way like that.

while(true)
{
period = checkIfPeriodChanged();
...
performTask
...
Sleep(period);
}

That works fine, until I want to change period time. For example, when
I set period to 15min, and after performTask I change it again to
1min, I have to wait all ~15min, to put my new settings (1min) into
operation. Is there any way, to break Sleep(period), or should I do it
in very different way? I looked at System.Threading.Timer, but i''m not
sure if it''ll be working, becouse I don''t know how long time execute
performTask will take (differences can be rather wide - from couple
seconds to few minutes).

I will be gratefull for any kind of help.
Masanobu Suga

推荐答案

10月3日上午11点22分,suga.masan ... @ gmail.com写道:


< snip>
On Oct 3, 11:22 am, suga.masan...@gmail.com wrote:

<snip>

这样可行,直到我想改变时间段。例如,当

我将周期设置为15分钟,并且在执行任务后我再次将其更改为

1分钟,我必须等待所有~15分钟,才能设置我的新设置( 1分钟进入

操作。有没有办法,打破睡眠(期间),或者我应该以非常不同的方式做到这一点


That works fine, until I want to change period time. For example, when
I set period to 15min, and after performTask I change it again to
1min, I have to wait all ~15min, to put my new settings (1min) into
operation. Is there any way, to break Sleep(period), or should I do it
in very different way?



而不是使用Thread.Sleep,使用Monitor.Wait,break"它与

Monitor.Pulse。


Jon

Instead of using Thread.Sleep, use Monitor.Wait, "breaking" it with
Monitor.Pulse.

Jon


< su * **********@gmail.com留言

新闻:11 ********************** @ 19g2000hsx.googlegro ups.com ...
<su***********@gmail.comwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...

Hello group。


我有线程,我在其中执行特定任务环。我可以通过复选框指定

,在什么时间段完成任务,ex。

立即,5分钟休息,15分钟休息等等。现在我有它

像这样完成。


while(true)

{

period = checkIfPeriodChanged();

...

performTask

...

睡眠(期间);

}


这样可行,直到我想改变时间段。例如,当

我将周期设置为15分钟,并且在执行任务后我再次将其更改为

1分钟,我必须等待所有~15分钟,才能设置我的新设置( 1分钟进入

操作。有没有办法,打破睡眠(期间),或者我应该以非常不同的方式做到这一点

?我查看了System.Threading.Timer,但我不是。

确定它是否会正常工作,因为我不知道执行了多长时间

performTask将采取(差异可以相当广泛 - 从几个

秒到几分钟)。


我会感激任何帮助。

Masanobu Suga
Hello group.

I have thread in which I perform specific task in loop. I can specify,
by check box, in what periods of time that task should be done, ex.
instantly, with 5min break, with 15min break etc. For now I had it
done in way like that.

while(true)
{
period = checkIfPeriodChanged();
...
performTask
...
Sleep(period);
}

That works fine, until I want to change period time. For example, when
I set period to 15min, and after performTask I change it again to
1min, I have to wait all ~15min, to put my new settings (1min) into
operation. Is there any way, to break Sleep(period), or should I do it
in very different way? I looked at System.Threading.Timer, but i''m not
sure if it''ll be working, becouse I don''t know how long time execute
performTask will take (differences can be rather wide - from couple
seconds to few minutes).

I will be gratefull for any kind of help.
Masanobu Suga



我想* performTask *启动另一个线程来运行任务,为什么不用

只需使用Thread.Join而不是Sleep?


Willy。


I suppose *performTask* starts another thread to run the task, why not
simply use Thread.Join instead of Sleep?

Willy.


su *********** @ gmail.com 写道:

[...]

while(true)

{

period = checkIfPeriodChanged();

...

performTask

...

睡眠(期间);

}


这样可行,直到我想改变时间段。例如,当

我将周期设置为15分钟,并且在执行任务后我再次将其更改为

1分钟,我必须等待所有~15分钟,才能设置我的新设置( 1分钟进入

操作。有没有办法,打破睡眠(期间),或者我应该以非常不同的方式做到这一点


[...]
while(true)
{
period = checkIfPeriodChanged();
...
performTask
...
Sleep(period);
}

That works fine, until I want to change period time. For example, when
I set period to 15min, and after performTask I change it again to
1min, I have to wait all ~15min, to put my new settings (1min) into
operation. Is there any way, to break Sleep(period), or should I do it
in very different way?



一些建议:


1)如果你想要暂停但仍然有警觉,你

可以使用WaitHandle(例如AutoResetEvent)并使用

超时(例如period)调用WaitOne()。这类似于Monitor方法Jon

提及;这只是做同样事情的另一种方式(假设你使用带有超时的Monitor.Wait()重载

)。


2 )恕我直言,如果目标是以特定的

间隔运行特定任务,并且您希望用户能够修改间隔,那么您的当前实施是不是'无论如何,这真的是最好的。


我认为最好只使用Forms.Timer类在特定时间内引发

事件间隔,然后使用

BackgroundWorker或类似的来运行任务。而不是让一个

线程坐在那里等待时间段的变化或

到期,而不是你只需要更新UI中的定时器时间段

对用户操作的响应,以及由计时器对象引发的事件

分配一个线程(通过BackgroundWorker或类似工具)来实际完成任务。


Pete

A couple of suggestions:

1) If you want to block with a timeout but still be alertable, you
can use a WaitHandle (eg AutoResetEvent) and call WaitOne() with a
timeout (eg "period"). This is similar to the Monitor method Jon
mentions; it''s just a different way of doing the same thing (assuming
you use the Monitor.Wait() overload with a timeout).

2) IMHO, if the goal is to run a specific task at specific
intervals, and you want the user to be able to modify the interval, your
current implementation isn''t really the best anyway.

I think it would be better to just use the Forms.Timer class to have an
event raised on a specific time interval, and then use a
BackgroundWorker or similar to run the task. Rather than having a
thread sitting around waiting to see if the time period changes or
expires, instead you would simply have the UI update the timer period in
response to user actions, and in the event raised by the timer object
assign a thread (via BackgroundWorker or similar) to actually do the task.

Pete


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

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