在t0 + dt处举办活动 [英] Raising an event at t0+dt

查看:67
本文介绍了在t0 + dt处举办活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我必须在.NET / C#中编写一个执行某项任务的算法但是必须在完成任务后停止
dT秒。类似于:


dT = 30秒;

for(;;){

t0 = GetCurrentTime();

for(i = 0; ...){

执行一些操作;

if(GetCurrentTime()t0 + dT)

休息;

}

}


我打算在t0 + dT举办活动,但我不喜欢不知道它是否合适并且

最好的解决方案...


有什么想法吗?


Oriane

解决方案

2007年6月5日星期二00:58:30 -0700,Oriane<或**** @ guermantes。 frwrote:


我必须在.NET / C#中编写一个执行某项任务的算法,但是在完成任务后要停止

dT秒。类似于:


dT = 30秒;

for(;;){

t0 = GetCurrentTime();

for(i = 0; ...){

执行一些操作;

if(GetCurrentTime()t0 + dT)

休息;

}

}


我打算在t0 + dT举办活动,但我不喜欢不知道它是否合适

和最佳解决方案......



制作dT将TimeSpan值设置为30秒,然后设置t0。 a

DateTime值,在循环之前将其设置为DateTime.Now。那么

当然在另一个地方使用DateTime.Now你有GetCurrentTime,并且

你们都已经设置好了。


我想你可以改为使用计时器和某种线程间的通信来处理时间,但假设你的执行一些

操作。这是非常重要的,我认为只需轮询每次操作之间的当前

时间。


Pete


你好彼得,


" Peter Duniho" < Np ********* @nnowslpianmk.coma ?? crit dans le message de

news:op *************** @ petes-computer.local ...

2007年6月5日星期二00:58:30 -0700,Oriane<或**** @ guermantes.frwrote:


我想你可以改为使用计时器和某种线程间的

通信来处理时间,



无论如何我会有一个多线程应用程序。我想过使用一个

计时器,但我必须在第一个信号后销毁它...


但是假设你的执行一些操作这是非常重要的,我认为只需轮询每次操作之间的当前时间就可以了。



所以你可能是对的,但我必须补充说执行一些操作

不会花太长时间。我的意思是,如果我等待t0 + dt,我将使用

来使线程在for循环中大部分时间处于休眠状态。这就是为什么它会为了准备而更加优雅。在

循环之前在t0 + dt处引发的事件,在执行操作后退出循环(所以在大多数

的情况下),然后赶上关闭工作的事件...所以Timer是一个很好的

解决方案...


Pete


6月5日下午12:22,Oriane < ori ... @ guermantes.frwrote:


Hello Peter,

On Tue,05 Jun 2007 00:58:30 - 0700,Oriane< ori ... @ guermantes.frwrote:


我想你可以改为使用计时器和某种类型的线程间

处理时间的通信,



无论如何我会有一个多线程应用程序。我想过使用一个

计时器,但我必须在第一个信号后销毁它...



它仍然是将这个特定的部分变得更容易单个

线程,我已经想到了。


....但是假设你的执行一些操作是非平凡的,我认为它是


罚款只是轮询当前时间每次操作之间。



所以你可能是对的,但我必须补充说执行一些

操作。我不会花太长时间。我的意思是,如果我等待t0 + dt,我必须让线程在for循环中大部分时间都处于休眠状态。



目前尚不清楚你想要在t0 + dt发生什么。如果这个点只是

让它暂停 - 即如果你到那个时候就不要继续 -

然后你不需要睡觉。只需在每次循环后再进行检查,如果你已达到最大值b / b
时间,那就挽救了。如果你碰巧在那段时间之前完成,我希望你能够直接返回。


如果这不是也许你可以给我们一些更多

的信息?


这就是为什么准备更优雅的原因在循环之前在

t0 + dt处引发的事件,在执行操作后执行循环(所以在大多数情况下是这样),然后抓住关闭工作的事件...

所以Timer是一个很好的解决方案...



这对我来说听起来更复杂 - 一个解决方案在
中搜索比你真正试图解决的更难的问题。


Jon


Hi there,

I have to write an algorithm in .NET/C# which perform some task but has to
be stopped after an intreval of dT seconds. Something like:

dT = 30sec;
for (;;) {
t0 = GetCurrentTime ();
for (i=0;...) {
perform some operations;
if ( GetCurrentTime () t0 + dT)
break;
}
}

I intend to raise an event at t0+dT, but I don''t know if it is posssible and
the best solution...

Any idea ?

Oriane

解决方案

On Tue, 05 Jun 2007 00:58:30 -0700, Oriane <or****@guermantes.frwrote:

I have to write an algorithm in .NET/C# which perform some task but has
to be stopped after an intreval of dT seconds. Something like:

dT = 30sec;
for (;;) {
t0 = GetCurrentTime ();
for (i=0;...) {
perform some operations;
if ( GetCurrentTime () t0 + dT)
break;
}
}

I intend to raise an event at t0+dT, but I don''t know if it is posssible
and the best solution...

Make "dT" a TimeSpan value, set it to 30 seconds, then make "t0" a
DateTime value, setting it to DateTime.Now before the loop. Then of
course use DateTime.Now in the other place you have "GetCurrentTime", and
you''re all set.

I suppose you could instead use a timer and some sort of inter-thread
communication to handle the timing, but assuming your "perform some
operations" is non-trivial, I think it''s fine to just poll the current
time between each operation.

Pete


Hello Peter,

"Peter Duniho" <Np*********@nnowslpianmk.coma ??crit dans le message de
news:op***************@petes-computer.local...
On Tue, 05 Jun 2007 00:58:30 -0700, Oriane <or****@guermantes.frwrote:

I suppose you could instead use a timer and some sort of inter-thread
communication to handle the timing,

Yes anyway I will have a multi-threading application. I thought of using a
Timer, but I would have to destroy it after the first signal...

but assuming your "perform some operations" is non-trivial, I think it''s
fine to just poll the current time between each operation.

So you are probably right, but I must add that the "perform some operations"
won''t take me too long. What I mean is that if I wait for t0+dt, I will have
to make the thread sleep most of the time in the for loop. That''s why it
would be more elegant to "prepare" an event to be raised at t0+dt before the
loop, quit the loop after the operations have been performed (so in most of
the case), then catch the event to close the job... So the Timer is a good
solution...

Pete


On Jun 5, 12:22 pm, "Oriane" <ori...@guermantes.frwrote:

Hello Peter,
On Tue, 05 Jun 2007 00:58:30 -0700, Oriane <ori...@guermantes.frwrote:

I suppose you could instead use a timer and some sort of inter-thread
communication to handle the timing,


Yes anyway I will have a multi-threading application. I thought of using a
Timer, but I would have to destroy it after the first signal...

It''s still going to be easier not to make this particular part single
threaded, I''d have thought.

....but assuming your "perform some operations" is non-trivial, I
think it''s

fine to just poll the current time between each operation.


So you are probably right, but I must add that the "perform some
operations" won''t take me too long. What I mean is that if I wait for t0+dt, I
will have to make the thread sleep most of the time in the for loop.

It''s not clear what you want to happen at t0+dt. If the point was just
to make it a timeout - i.e. don''t keep going if you get to that time -
then you don''t need to sleep. Just put the check in after each
iteration of the loop, bailing out if you''ve reached that maximum
time. If you happen to finish before that time, I''d expect you to be
able to just return anyway.

If this isn''t the case, perhaps you could give us some more
information?

That''s why it would be more elegant to "prepare" an event to be raised at
t0+dt before the loop, quit the loop after the operations have been
performed (so in most of the case), then catch the event to close the job...
So the Timer is a good solution...

That sounds significantly more complicated to me - a solution in
search of a harder problem than you''re really trying to solve.

Jon


这篇关于在t0 + dt处举办活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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