定时器在实际的截止时间之前几毫秒触发 [英] Timer fires a few milliseconds before the actual Due-Time

查看:75
本文介绍了定时器在实际的截止时间之前几毫秒触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS 2005测试版 - C#


问题:计时器在实际到期时间前几毫秒触发


让比如用以下方式创建一个计时器:

System.Threading.Timer m_timer = null;


让'声明一个常量

Int32 m_TimePeriod = 10000;

在Initialize方法中:

m_timer = new System.Threading.Timer(new TimerCallback(XXXTimerProc));

m_timer.Change(m_TimePeriod,0);

在窗体的WndProc中:

获取当前时间并存储在m_dtCurrentTime中


//更改截止日期和期间以启用计时器

m_timer.Change(m_TimePeriod,0);

在XXXTimerProc:

检查(当前时间 - m_dtCurrrentTime)> = m_TimePeriod

{

//执行某些操作


::

::


//更改截止时间和期限以禁用计时器

m_timer。更改(系统。 Threading.Timeout.Infinite,

System.Threading.Timeout.Infinite);

}

在表格的WndProc中,定时器'调用Change方法将

DueTime设置为m_TimePeriod。这启用了计时器。但是在m_TimePeriod之前,Timer会被激活几个b $ b毫秒。因此,在XXXTimerProc中,用于检查DateTime值之间差异的

条件失败,并且

不会发生必要的操作。


我有什么问题或者它是VS 2005 Beta中的错误吗?

I am using VS 2005 Beta - C#

Problem: The Timer fires a few milliseconds before the actual Due-Time

Let''s say a timer is created in the following manner:
System.Threading.Timer m_timer = null;

Let''s declare a constant
Int32 m_TimePeriod = 10000;
In the Initialize method:
m_timer = new System.Threading.Timer(new TimerCallback(XXXTimerProc));
m_timer.Change(m_TimePeriod, 0);
In WndProc of Form:
Get the current time and store in m_dtCurrentTime

// Change the Due-Time and Period to enable the timer
m_timer.Change(m_TimePeriod, 0);
In the XXXTimerProc:
Check if ( Current Time - m_dtCurrrentTime) >= m_TimePeriod
{
// Perform some action

::
::

// Change the Due-Time and Period to disable the timer
m_timer.Change(System.Threading.Timeout.Infinite,
System.Threading.Timeout.Infinite);
}
In the WndProc of the Form, the Timer''s Change method is called to set the
DueTime to m_TimePeriod. This enables the timer. But the Timer is fired a few
milliseconds before m_TimePeriod. As a result, In the XXXTimerProc, the
condition for checking the difference between the DateTime values fails and
the neccesary actions do not take place.

Is there something wrong that I am doing or Is it a bug in VS 2005 Beta?

推荐答案




首先,你永远不会完全执行事件

指定,winXX不是实时操作系统所以当时间到了

执行定时器只是简单地生成一个事件,处理程序将在线程池中的一个线程中执行


你可以想象所有这些操作都需要时间。


据说比较if(当前时间 - m_dtCurrrentTime)将

永远不会是准确的。

真的不要没有看到比较的重点,毕竟你从计时器调用

这个,检查计时器经过的时间有什么意义?

为什么是y你打电话给改变两次,你的问题可能就在哪里。


欢呼,


-

伊格纳西奥Machin,

ignacio.machin AT dot.state.fl.us

Florida Department of Transportation

" HL" < HL@discussions.microsoft.com>在消息中写道

news:24 ********************************** @ microsof t.com ...
Hi,

First of all you will NEVER get the event executed exactly as you
especified, winXX is not a real time OS so what is done when the time to
execute the timer is simply generate an event, the handler will be executed
in a thread frm the threadpool.
As you can imagine all this actions take time.

With that said the comparision if ( Current Time - m_dtCurrrentTime) will
never be exact.
And really don''t see the point of the comparision, after all you are calling
this from a timer, what is the point to check the timer elapsed time ?
Why are you calling Change twice , there is where your problem may be.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"HL" <HL@discussions.microsoft.com> wrote in message
news:24**********************************@microsof t.com...
我正在使用VS 2005 Beta - C#

问题:计时器在实际到期时间之前几毫秒触发

假设计时器是按以下方式创建的:
System.Threading.Timer m_timer = null;

让我们声明一个常量
Int32 m_TimePeriod = 10000;

在Initialize方法中:
m_timer = new System.Threading.Timer(new TimerCallback(XXXTimerProc));
m_timer.Change(m_TimePeriod,0);

在窗体的WndProc中:
获取当前时间并存储在m_dtCurrentTime中

//更改截止时间和期限以启用计时器
m_timer .Change(m_TimePeriod,0);

在XXXTimerProc:
检查是否(当前时间 - m_dtCurrrentTime)> = m_TimePeriod
{
//执行一些动作

::
::

//更改截止时间和期间以禁用计时器
m_timer.Change(System.Threading.Timeout.Infinite,
System.Threading.Timeout.Infinite);
}

在表格的WndProc中,调用Timer的Change方法将
DueTime设置为m_TimePeriod。这启用了计时器。但是在m_TimePeriod之前,Timer会被激活几毫秒。因此,在XXXTimerProc中,检查DateTime值之间的差异的条件将失败

不会发生必要的操作。

是否存在我正在做的事情是错误的还是VS 2005 Beta中的错误?
I am using VS 2005 Beta - C#

Problem: The Timer fires a few milliseconds before the actual Due-Time

Let''s say a timer is created in the following manner:
System.Threading.Timer m_timer = null;

Let''s declare a constant
Int32 m_TimePeriod = 10000;
In the Initialize method:
m_timer = new System.Threading.Timer(new TimerCallback(XXXTimerProc));
m_timer.Change(m_TimePeriod, 0);
In WndProc of Form:
Get the current time and store in m_dtCurrentTime

// Change the Due-Time and Period to enable the timer
m_timer.Change(m_TimePeriod, 0);
In the XXXTimerProc:
Check if ( Current Time - m_dtCurrrentTime) >= m_TimePeriod
{
// Perform some action

::
::

// Change the Due-Time and Period to disable the timer
m_timer.Change(System.Threading.Timeout.Infinite,
System.Threading.Timeout.Infinite);
}
In the WndProc of the Form, the Timer''s Change method is called to set the
DueTime to m_TimePeriod. This enables the timer. But the Timer is fired a
few
milliseconds before m_TimePeriod. As a result, In the XXXTimerProc, the
condition for checking the difference between the DateTime values fails
and
the neccesary actions do not take place.

Is there something wrong that I am doing or Is it a bug in VS 2005 Beta?



我也看到了这个问题,我还没有''找到了解释。


见内联评论:


Ignacio Machin(.NET / C#MVP)" < ignacio.machin AT dot.state.fl.us>在消息新闻中写了

:或者************** @ tk2msftngp13.phx.gbl ...
I have also seen this problem and I haven''t found an explanation.

See in-line comments:

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:OR**************@tk2msftngp13.phx.gbl...

首先,你永远不会完全按照你指定的方式执行事件,winXX不是实时操作系统,所以当执行计时器的时间只是生成一个事件,处理程序将在线程池中的线程中执行。
您可以想象所有这些操作都需要时间。



我们意识到Windows不是实时操作系统,我会理解,如果

计时器在几毫秒内熄灭或者即便到了几秒钟,但问题是,它很快就会消失。如果我设定一个计时器从现在起减少50,000

毫秒,为什么它会从现在开始49,998毫秒?

我能想到的唯一解释是Windows时间服务

将时间向前调整几毫秒,但这并不会影响我们点火的时间。那只是一个猜测。

据说,比较if(当前时间 - m_dtCurrrentTime)将永远不会是准确的。
并且真的没有看到这一点比较,毕竟你是从计时器调用这个,有什么意义检查计时器已经过去的时间?


在我们的例子中,我们收集的东西必须在特定的b
次发生。我们遍历那个集合并找到最近的时间然后

计算从现在到项目行动时间之间的毫秒数。

然后我们设置一个计时器进入那个毫秒数。当

计时器响起时,我们再次浏览列表,期望找到至少

一个项目,其动作时间已过,但有时我们不会因为

计时器过早地停止了几毫秒。


在我们的例子中,它只是意味着我们只重置了几个时间

毫秒,但我仍然想知道发生了什么。

John Vottero


为什么你打电话给Change两次,那里是你的问题所在。

欢呼,

- Ignacio Machin,
ignacio.machin at dot.state.fl.us <佛罗里达州交通局

HL < HL@discussions.microsoft.com>在消息中写道
新闻:24 ********************************** @ microsof t.com。 ..
Hi,

First of all you will NEVER get the event executed exactly as you
especified, winXX is not a real time OS so what is done when the time to
execute the timer is simply generate an event, the handler will be
executed in a thread frm the threadpool.
As you can imagine all this actions take time.

We realize that Windows isn''t a real time OS, I would understand if the
timer went off a few milliseconds too late or even seconds late but, the
problem is it''s going off too soon. If I set a timer to go off 50,000
milliseconds from now, why would it go off 49,998 milliseconds from now?
The only explanation that I can think of it that the Windows Time service
adjusts the time forward a few milliseconds but that doesn''t affect when
timers we fire. That''s just a guess.

With that said the comparision if ( Current Time - m_dtCurrrentTime) will
never be exact.
And really don''t see the point of the comparision, after all you are
calling this from a timer, what is the point to check the timer elapsed
time ?
In our case, we have a collection of things that have to happen at specific
times. We walk through that collection and find the nearest time and then
calculate the number of milliseconds between now and the items action time.
Then we set a timer to go off in that number of milliseconds. When the
timer goes off, we walk through the list again expecting to find at least
one item with an action time that has past but, sometimes we don''t because
the timer went off a few milliseconds too soon.

In our case, it just means that we reset the time for just a few
milliseconds but I would still like to know what''s going on.
John Vottero


Why are you calling Change twice , there is where your problem may be.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"HL" <HL@discussions.microsoft.com> wrote in message
news:24**********************************@microsof t.com...
我正在使用VS 2005 Beta - C#

问题:计时器在实际到期时间前几毫秒启动

让'假设计时器是按以下方式创建的:
System.Threading.Timer m_timer = null;

让我们声明一个常量
Int32 m_TimePeriod = 10000;

在Initialize方法中:
m_timer = new System.Threading.Timer(new TimerCallback(XXXTimerProc));
m_timer.Change(m_TimePeriod,0);

在窗体的WndProc中:
获取当前时间并存储在m_dtCurrentTime中

//更改截止时间和期间以启用计时器
m_timer.Change(m_TimePeriod ,0);

在XXXTimerProc:
检查是否(当前时间 - m_dtCurrrentTime)> = m_TimePeriod
{
//执行某些动作

::
::

//更改截止时间和期间以禁用计时器 m_timer.Change(System.Threading.Timeout.Infinite,
System.Threading.Timeout.Infinite);
}

在窗体的WndProc中,定时器''调用更改方法将
DueTime设置为m_TimePeriod。这启用了计时器。但是在m_TimePeriod之前,Timer会被激活几毫秒。因此,在XXXTimerProc中,检查DateTime值之间的差异的条件将失败

不会发生必要的操作。

是否存在我正在做的事情是错误的还是VS 2005 Beta中的错误?
I am using VS 2005 Beta - C#

Problem: The Timer fires a few milliseconds before the actual Due-Time

Let''s say a timer is created in the following manner:
System.Threading.Timer m_timer = null;

Let''s declare a constant
Int32 m_TimePeriod = 10000;
In the Initialize method:
m_timer = new System.Threading.Timer(new TimerCallback(XXXTimerProc));
m_timer.Change(m_TimePeriod, 0);
In WndProc of Form:
Get the current time and store in m_dtCurrentTime

// Change the Due-Time and Period to enable the timer
m_timer.Change(m_TimePeriod, 0);
In the XXXTimerProc:
Check if ( Current Time - m_dtCurrrentTime) >= m_TimePeriod
{
// Perform some action

::
::

// Change the Due-Time and Period to disable the timer
m_timer.Change(System.Threading.Timeout.Infinite,
System.Threading.Timeout.Infinite);
}
In the WndProc of the Form, the Timer''s Change method is called to set
the
DueTime to m_TimePeriod. This enables the timer. But the Timer is fired a
few
milliseconds before m_TimePeriod. As a result, In the XXXTimerProc, the
condition for checking the difference between the DateTime values fails
and
the neccesary actions do not take place.

Is there something wrong that I am doing or Is it a bug in VS 2005 Beta?




为什么要'你使用的是一个不断运行的线程Sleep(0-1)和

会分配任务吗?它是最精确的方式,它只需要更加困难......


" John Vottero" <乔** @ mvpsi.com> schrieb im Newsbeitrag

新闻:uT ************** @ TK2MSFTNGP09.phx.gbl ...
why don''t you use a thread instead that runs constantly Sleep(0-1) and
distributes tasks as the come to be? Its the most precise way to do it and
is only a tick more difficult...

"John Vottero" <Jo**@mvpsi.com> schrieb im Newsbeitrag
news:uT**************@TK2MSFTNGP09.phx.gbl...
我也见过这个问题,我还没有找到解释。

请参阅内嵌评论:

Ignacio Machin(.NET / C#MVP)" < ignacio.machin AT dot.state.fl.us>
在消息新闻中写道:或者************** @ tk2msftngp13.phx.gbl ...
I have also seen this problem and I haven''t found an explanation.

See in-line comments:

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>
wrote in message news:OR**************@tk2msftngp13.phx.gbl...


首先,你永远不会完全按照你指定的方式执行事件,winXX不是实时操作系统所以当时间到了<执行定时器只是生成一个事件,处理程序将在线程池中的线程中执行。
你可以想象所有这些操作都需要时间。
Hi,

First of all you will NEVER get the event executed exactly as you
especified, winXX is not a real time OS so what is done when the time to
execute the timer is simply generate an event, the handler will be
executed in a thread frm the threadpool.
As you can imagine all this actions take time.



我们意识到Windows不是实时操作系统,我会理解,如果
计时器在太晚甚至几秒钟后熄灭几毫秒但是,
问题是它的'太快了。如果我设置一个计时器从现在开始50,000毫秒,为什么它会从现在开始49,998毫秒?
我能想到的唯一解释是Windows时间服务
调整时间几毫秒,但这并不影响我们开火的定时器。这只是一个猜测。


We realize that Windows isn''t a real time OS, I would understand if the
timer went off a few milliseconds too late or even seconds late but, the
problem is it''s going off too soon. If I set a timer to go off 50,000
milliseconds from now, why would it go off 49,998 milliseconds from now?
The only explanation that I can think of it that the Windows Time service
adjusts the time forward a few milliseconds but that doesn''t affect when
timers we fire. That''s just a guess.


据说,比较if(当前时间 - m_dtCurrrentTime)
将永远不会是准确的。
真的没有看到比较的重点,毕竟你是从计时器调用这个,检查计时器已经过去的时间是什么时候?

With that said the comparision if ( Current Time - m_dtCurrrentTime)
will never be exact.
And really don''t see the point of the comparision, after all you are
calling this from a timer, what is the point to check the timer elapsed
time ?


<在我们的案例中,我们收集了必须在特定时间发生的事情。我们遍历该集合并找到最近的时间
然后计算从现在到项目之间的毫秒数
动作时间。然后我们将计时器设置为在该毫秒数内完成。
当计时器关闭时,我们再次遍历列表,期望找到至少一个具有已超过的动作时间的项目,但是,有时候我们不会因为计时器过早地停止几毫秒。

在我们的情况下,它只是意味着我们只将时间重置几毫秒
毫秒但是我仍然想知道发生了什么。

John Vottero



In our case, we have a collection of things that have to happen at
specific times. We walk through that collection and find the nearest time
and then calculate the number of milliseconds between now and the items
action time. Then we set a timer to go off in that number of milliseconds.
When the timer goes off, we walk through the list again expecting to find
at least one item with an action time that has past but, sometimes we
don''t because the timer went off a few milliseconds too soon.

In our case, it just means that we reset the time for just a few
milliseconds but I would still like to know what''s going on.
John Vottero



你为什么要叫Change两次你的问题可能存在。

欢呼,

- Ignacio Machin,
ignacio.machin at dot.state.fl。我们
佛罗里达州交通部

" HL" < HL@discussions.microsoft.com>在消息中写道
新闻:24 ********************************** @ microsof t.com。 ..


Why are you calling Change twice , there is where your problem may be.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"HL" <HL@discussions.microsoft.com> wrote in message
news:24**********************************@microsof t.com...
我正在使用VS 2005 Beta - C#

问题:计时器在实际到期时间前几毫秒启动

让'假设计时器是按以下方式创建的:
System.Threading.Timer m_timer = null;

让我们声明一个常量
Int32 m_TimePeriod = 10000;

在Initialize方法中:
m_timer = new System.Threading.Timer(new TimerCallback(XXXTimerProc));
m_timer.Change(m_TimePeriod,0);

在窗体的WndProc中:
获取当前时间并存储在m_dtCurrentTime中

//更改截止时间和期间以启用计时器
m_timer.Change(m_TimePeriod ,0);

在XXXTimerProc:
检查是否(当前时间 - m_dtCurrrentTime)> = m_TimePeriod
{
//执行某些动作

::
::

//更改截止时间和期间以禁用计时器 m_timer.Change(System.Threading.Timeout.Infinite,
System.Threading.Timeout.Infinite);
}

在窗体的WndProc中,定时器''调用更改方法将
DueTime设置为m_TimePeriod。这启用了计时器。但是在m_TimePeriod之前几分钟就会触发Timer。因此,在XXXTimerProc中,检查DateTime值之间的差异的条件将失败

不会发生必要的操作。

是否存在我正在做的事情是错误的还是VS 2005 Beta中的错误?
I am using VS 2005 Beta - C#

Problem: The Timer fires a few milliseconds before the actual Due-Time

Let''s say a timer is created in the following manner:
System.Threading.Timer m_timer = null;

Let''s declare a constant
Int32 m_TimePeriod = 10000;
In the Initialize method:
m_timer = new System.Threading.Timer(new TimerCallback(XXXTimerProc));
m_timer.Change(m_TimePeriod, 0);
In WndProc of Form:
Get the current time and store in m_dtCurrentTime

// Change the Due-Time and Period to enable the timer
m_timer.Change(m_TimePeriod, 0);
In the XXXTimerProc:
Check if ( Current Time - m_dtCurrrentTime) >= m_TimePeriod
{
// Perform some action

::
::

// Change the Due-Time and Period to disable the timer
m_timer.Change(System.Threading.Timeout.Infinite,
System.Threading.Timeout.Infinite);
}
In the WndProc of the Form, the Timer''s Change method is called to set
the
DueTime to m_TimePeriod. This enables the timer. But the Timer is fired
a few
milliseconds before m_TimePeriod. As a result, In the XXXTimerProc, the
condition for checking the difference between the DateTime values fails
and
the neccesary actions do not take place.

Is there something wrong that I am doing or Is it a bug in VS 2005 Beta?





这篇关于定时器在实际的截止时间之前几毫秒触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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