Dav*_*ave 11 .net c# timer winforms
我有一个应用程序,它使用System.Timers.Timer对象来引发由主窗体(Windows窗体,C#)处理的事件.我的问题是,无论我设置.Interval(甚至1毫秒)有多短,我每秒最多得64次.
我知道Forms计时器的精度限制为55毫秒,但这是System.Timer变体,而不是Forms表单.
该应用程序占用1%的CPU,因此它绝对不受CPU限制.所以它所做的只是:
即使没有其他工作要做,_Count每秒最多增加64次.
这是一个"回放"应用程序,它必须复制进来的数据包,它们之间只有1-2毫秒的延迟,所以我需要能够每秒可靠地发射1000次的东西(尽管如果我能满足100的话)是CPU限制的,我不是).
有什么想法吗?
尝试多媒体计时器- 它们为硬件平台提供了尽可能高的准确性。这些计时器以比其他计时器服务更高的分辨率来安排事件。
您将需要以下 Win API 函数来设置计时器分辨率、启动和停止计时器:
[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);
Run Code Online (Sandbox Code Playgroud)
您还需要回调委托:
delegate void TimeProc(int id, int msg, int user, int param1, int param2);
Run Code Online (Sandbox Code Playgroud)
和定时器功能结构
[StructLayout(LayoutKind.Sequential)]
public struct TimerCaps
{
public int periodMin;
public int periodMax;
}
Run Code Online (Sandbox Code Playgroud)
用法:
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);
Run Code Online (Sandbox Code Playgroud)
和回调:
void TimerCallback(int id, int msg, int user, int param1, int param2)
{
// occurs every 1 ms
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1697 次 |
| 最近记录: |