Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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


VB
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[^]
Posted
Comments
Sergey Alexandrovich Kryukov 18-Sep-13 10:56am    
What type of timer do you use? Full name, please.
And how can we be sure that the methods you show are event handlers? Show more adequate code samples, where all relevant += operators are shown.
—SA
[no name] 18-Sep-13 10:57am    
Well I would suspect that whatever your actual question is, comes from "counter = Val(Label7.Text) - counter"

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
 
Share this answer
 
Comments
Ron Beyer 18-Sep-13 11:14am    
+5, also see my answer below, the highest resolution is greater than the timer tick he selected.
Sergey Alexandrovich Kryukov 18-Sep-13 11:25am    
Thank you, Ron.
—SA
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).
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Sep-13 11:29am    
5ed. Nevertheless, you can use time with much, much better resolution, if you design the code correctly. Imagine you need to animate some motion. Position of a ball, for example. You can simply use a thread for the scenario, which will give you somewhat randomized timing at the moment you need to refresh the picture. But for calculation of actual position, you can use the time from a very accurate, real-time System.Diagnostic.Stopwatch. Animation is smooth and timing precision is excellent, I'll guarantee it. But if you used the 15.6 ms quanta, the human vision would detect the lousy timing in motion immediately. See the point?
—SA
Ron Beyer 18-Sep-13 11:42am    
Yup, just remember your favorite big-screen movies run at 24 FPS, which is a new frame every 41.6ms. To your eye, the motion looks smooth because of (mostly) blur. The brain interpolates between frames a bit as well (cartoons). The wrong technique can make a 200 FPS animation look "choppy" while a good technique can make low FPS (original DOOM) look pretty damn good :) WPF and DirectX do this pretty well in the background with animation classes and some video cards even have motion blur on-board.
Sergey Alexandrovich Kryukov 18-Sep-13 12:32pm    
This is by far not so simple. I've read a cycle of article on human vision related to video, the results are quite revolutionary to usual understanding of it. And you are perfectly right about bad and good animation quality. I am talking about hand-made animation where you calculate each frame on the fly. Also look damn good, based on simplest technique. Here, the frame frequency is not even uniform, but if gives better result than with a timer...
—SA
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900