算在5毫秒? [英] Counting in 5 milliseconds ?

查看:75
本文介绍了算在5毫秒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
为什么不计算时间1秒到200?

数量不超过65 !!!!!!!!!!!!

b / b








定时器1间隔:5 ms

定时器2间隔:1000毫秒





hi Why is not counting time 1secend to 200 ?
Number is not more than 65 !!!!!!!!!!!!





Timer 1 interval : 5 ms
Timer 2 interval : 1000 ms


Public Class Form1

    Dim counter As Integer

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        counter = counter + 1
        Label7.Text = counter
    End Sub


    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
        Label9.Text = Val(Label7.Text)
        counter = Val(Label7.Text) - counter
    End Sub


End Class











link dl:

http:/ /www.mediafire.com/download/1k7vor1o05ybq22/ferq_metr_3.rar [ ^ ]

推荐答案

请参阅我的评论问题。



如果您的代码完全正常工作并且处理程序(再次,您应该表明您的方法实际上是处理程序)实际上被调用,它表明您正在使用 System.Windows.Forms.Timer 。其他计时器通常需要UI调用( Control.Invoke Control.BeginInvoke )来通知UI。



如果您使用此计时器类型,请忘记准确性。这种类型的创建是为了更加简单,而不是为了准确性,这是非常糟糕的。它几乎无法使用。典型的合理用法是这样的:一些事情发生一次,延迟几秒钟,闪光屏幕或提示。如果您需要一些准确性,请使用任何其他计时器( System.Threading.Timer System.Timers.Timer )或者考虑使用多线程,这更简单,更容易实现。您可以将它与非常准确的 System.Diagnostics.Stopwatch 结合使用。



-SA
Please see my comments to the question.

If your code works at all and the handlers (again, you should show that your methods are actually the handlers) are actually called, it suggests that you are using System.Windows.Forms.Timer. Other timers generally need UI invocation (Control.Invoke, Control.BeginInvoke) to notify the UI.

If you use this timer type, forget about accuracy. This type is created for extra simplicity, not for accuracy, which is notoriously bad. It is hardly usable. The typical reasonable usage is this: make something happen once with a delay of few seconds, something as a flash screen or hint. If you need some accuracy, use any of other timers (System.Threading.Timer, System.Timers.Timer) or consider using multithreading, which is more straightforward and easier to implement. You can use it in combination with very accurate System.Diagnostics.Stopwatch.

—SA


除了上面的SA答案,Windows计时器的最高分辨率是15.6毫秒。 Windows不是实时操作系统,而且是多任务处理,因此当您的计时器运行,并且您的程序正在运行时,其他程序和计时器需要运行,因此Windows也会将时间委托给它们。



无论你将计时器设置为什么,15.6ms是你实际得到的最低值(如果你幸运的话)。
In addition to SA's answer above, the highest resolution you can get on a Windows timer is 15.6 milliseconds. Windows is not a real-time operating system and it is multitasking, so while your timer is operating, and your program is running, other programs and timers need to run so windows delegates time to those as well.

No matter what you set the timer to, 15.6ms is the lowest value you will actually get (if you are lucky).


有一对你需要注意的事情,第一个是Tick事件不会在你指定的时间间隔发生。 Tick事件将在您指定的时间间隔之后发生,但确切的时间将取决于您的计算机的速度,以及它当时正在做什么 - 因为Timers不是先发制人(这意味着它们不会接管您的应用程序)当计时器Ticks)并且如果系统太忙而无法处理Tick事件,并且直到下一个事件发生之后才会变为空闲,你将不会得到两个相邻的Tick事件:相反,你会失去 打勾。如果出于任何原因在您的代码中发生这种情况,那么第二个数字总是不同步并且永远不会重新进入。



接下来的事情是定时器不是优先级。仅仅因为你给它们不同的名字并不意味着Timer1 Tick事件总是在Timer2事件发生之前发生。再一次,一旦失去了步骤,你的代码就无法纠正它。



我怎么做:

使用一个计时器设置为适当的间隔。

将DateTime设置为当前时间加上1000ms - 将其设置为check-next-time值

在tick事件中,检查实际时间使用DateTime.Now对着check-next-time值而不是一个计数器,如果它已经过去,就像当时一样,然后将1000ms加到check-next-time-time。
There are a couple of things you need to be aware of here, and the first one is that Tick events do not happen at exactly the interval you specify. A Tick event will occur after the interval you specify, but exactly when will depend on the speed of your computer, and what else it is doing at the time - because Timers are not pre-emptive (which means they do not take over your application when the timer "Ticks") and if the system is too busy to handle a Tick event when it happens, and does not become free until after the next one, you will not get two Tick events in close proximity: instead you will "lose" a Tick. If that happens in your code for any reason, then the second number always gets out of synch and will never get back in.

The next thing is that timers are not prioritized. Just because you have given them different names does not mean that the Timer1 Tick event will always happen before the Timer2 event does. Again, as soon at this gets out of step your code cannot correct for it.

How I would do it:
Use one timer, set at an appropriate interval.
Set a DateTime to the current time plus 1000ms - call this the check-next-time value
In the tick event, check the actual time using DateTime.Now against the check-next-time value rather than a counter and if it has passed, act as if it was at the time, and then add 1000ms to the check-next-time time.


这篇关于算在5毫秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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