System.Timers.Timer的只提供每秒最大64帧 [英] System.Timers.Timer only gives maximum 64 frames per second

查看:234
本文介绍了System.Timers.Timer的只提供每秒最大64帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用System.Timers.Timer的对象的应用程序,提高了由主表(的 Windows窗体,C#)。我的问题是,无论多么短暂我设置.Interval(甚至1 毫秒)。我获得了最高每秒64次。

I have an application that uses a System.Timers.Timer object to raise events that are processed by the main form (Windows Forms, C#). My problem is that no matter how short I set the .Interval (even to 1 ms) I get a max of 64 times per second.

我知道窗体定时器具有55 NBSP;毫秒精度的限制,但这是System.Timer变型,而不是形成一个

I know the Forms timer has a 55 ms accuracy limit, but this is the System.Timer variant, not the Forms one.

应用程序位于1%的CPU,所以它绝对不是CPU密集型的。因此,所有它做的是:

The application sits a 1% CPU, so it's definitely not CPU-bound. So all it's doing is:

  • 设置定时器1 NSP; MS
  • 当事件触发,增加一个_COUNT变量
  • 设置到1 NSP;再次毫秒,重复

_COUNT得到增加最多64次,第二次,即使没有其他的工作要做。

_Count gets incremented a maximum of 64 times a second even when there's no other work to do.

这是一个播放应用程序具有复制数据包进来用尽可能少的1-2 它们之间的毫秒的延迟,所以我需要的东西,可以可靠地触发1000次,第二次左右(虽然我会解决100,如果我是CPU限制,我没有)。

This is an "playback" application that has to replicate packets coming in with as little as 1-2 ms delay between them, so I need something that can reliably fire 1000 times a second or so (though I'd settle for 100 if I was CPU bound, I'm not).

有什么想法?

推荐答案

尝试的多媒体计时器 - 他们提供可能的最高精度为硬件平台。这些定时器安排在一个更高的分辨率比其他计时器服务活动。

Try Multimedia Timers - they provide greatest accuracy possible for the hardware platform. These timers schedule events at a higher resolution than other timer services.

您将需要以下赢API函数来设置计时器的分辨率,启动和停止计时器:

You will need following Win API functions to set timer resolution, start and stop timer:

[DllImport("winmm.dll")]
private static extern int timeGetDevCaps(ref TimerCaps caps, int sizeOfTimerCaps);

[DllImport("winmm.dll")]
private static extern int timeSetEvent(int delay, int resolution, TimeProc proc, int user, int mode);

[DllImport("winmm.dll")]
private static extern int timeKillEvent(int id);

您还需要回调委托:

delegate void TimeProc(int id, int msg, int user, int param1, int param2);

和定时器功能结构

[StructLayout(LayoutKind.Sequential)]
public struct TimerCaps
{
    public int periodMin;
    public int periodMax;
}

用法:

TimerCaps caps = new TimerCaps();
// provides min and max period 
timeGetDevCaps(ref caps, Marshal.SizeOf(caps));
int period = 1;
int resolution = 1;
int mode = 0; // 0 for periodic, 1 for single event
timeSetEvent(period, resolution, new TimeProc(TimerCallback), 0, mode);

和回调:

void TimerCallback(int id, int msg, int user, int param1, int param2)
{
    // occurs every 1 ms
}

这篇关于System.Timers.Timer的只提供每秒最大64帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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