开发者

Timer speed Problem WPF

开发者 https://www.devze.com 2023-03-01 19:44 出处:网络
I am using timer to scroll the list. Every millisecond interval i am initiating the Tick event. Event though the animation looks very slow.

I am using timer to scroll the list. Every millisecond interval i am initiating the Tick event. Event though the animation looks very slow.

 repeatTimer_Tick(this, new EventArgs());
        repeatLeftTimer.Interval = TimeSpan.FromMilliseconds(1);
开发者_运维知识库        repeatLeftTimer.Tick += new EventHandler(repeatTimer_Tick);


The timers in windows have a resolution of several milliseconds, IIRC, something between 10 and 50 ms. You can't get the normal timer to tick every millisecond.


I would try to use an animation for what you're doing.

The .NET timer classes generally have a minimum interval of 15.6 ms. If you need a faster increment (down to 1ms) Windows provides a multimedia timer in the unmanaged API. You could import this timer and use it -- I've done it and it works.

I based my code off of this: http://www.codeproject.com/KB/miscctrl/lescsmultimediatimer.aspx

Should be most of what you need; I added some code to look at the high resolution timer and observe the time between ticks -- I was able to get it to 1ms, += 0.10 ms


You have two good and (relatively) easy options:

  1. Use an animation - WPF has a very versatile animation system that is likely to be able to do what you need.

  2. Use the CompositionTarget.Rendering Event, those are called as fast as possible (each frame) and you are responsible in each call to set the current status based on the current time (that is how the built in animations work)


If you need a high resolution then I would suggest looking into the Stopwatch class. Use the Frequency field and the ElapsedTicks property to get the resolution you are looking for. Note though that the resolution of the stopwatch will change depending on the hardware. Read the link to the msdn documentation for more information.

0

精彩评论

暂无评论...
验证码 换一张
取 消