System.Windows.Forms -> 55ms
System.Threading.Timer or System.Timers.Timer -> 10ms
자 그러면 1ms 의 정확성이 필요한 타이머를 사용할 필요 성이 있을때는?
구글을 뒤져보면 여러가지의 라이브러리 들이 나오는데 그 외에 XNA Timer(게임 프로그래밍에 사용되는 것으로 대체로 아주 높은
정확성)
유용한 라이브러리가 있는 줄 알고 온 사람은 낚였음... ㅎㅎ
검색을 생활화 합시다^^
//The timeSetEvent() function from the multi-media API provides for a very accurate periodic callback that is //auto-//adjusting and can go as low as 1msec. Here's some code to try it out:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication1 {
public partial class Form1 : Form {
private int mTimerId;
private TimerEventHandler mHandler; // NOTE: declare at class scope so garbage collector doesn't release it!!!
private int mTestTick;
private DateTime mTestStart;
public Form1() {
InitializeComponent();
label1 = new Label();
label1.Location = new Point(10, 5);
label2 = new Label();
label2.Location = new Point(10, 30);
this.Controls.Add(label1);
this.Controls.Add(label2);
}
protected override void OnLoad(EventArgs e) {
timeBeginPeriod(1);
mHandler = new TimerEventHandler(TimerCallback);
mTimerId = timeSetEvent(1, 0, mHandler, IntPtr.Zero, EVENT_TYPE);
mTestStart = DateTime.Now;
mTestTick = 0;
}
protected override void OnFormClosing(FormClosingEventArgs e) {
mTimerId = 0;
int err = timeKillEvent(mTimerId);
timeEndPeriod(1);
// Ensure callbacks are drained
System.Threading.Thread.Sleep(100);
}
private delegate void TestEventHandler(int tick, TimeSpan span);
private void TimerCallback(int id, int msg, IntPtr user, int dw1, int dw2) {
mTestTick += 1;
if ((mTestTick % 200) == 0 && mTimerId != 0)
this.BeginInvoke(new TestEventHandler(ShowTick), mTestTick, DateTime.Now - mTestStart);
}
private void ShowTick(int msec, TimeSpan span) {
label1.Text = msec.ToString();
label2.Text = span.TotalMilliseconds.ToString();
}
// P/Invoke declarations
private delegate void TimerEventHandler(int id, int msg, IntPtr user, int dw1, int dw2);
private const int TIME_PERIODIC = 1;
private const int EVENT_TYPE = TIME_PERIODIC;// + 0x100; // TIME_KILL_SYNCHRONOUS causes a hang ?!
[DllImport("winmm.dll")]
private static extern int timeSetEvent(int delay, int resolution, TimerEventHandler handler, IntPtr user, int eventType);
[DllImport("winmm.dll")]
private static extern int timeKillEvent(int id);
[DllImport("winmm.dll")]
private static extern int timeBeginPeriod(int msec);
[DllImport("winmm.dll")]
private static extern int timeEndPeriod(int msec);
}
}
//above source is from the MSDN
최근 덧글