求精确定时的方法(不是计时)~

xufzu123 2009-04-21 08:17:28
精确到1ms
...全文
59 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xufzu123 2009-04-24
  • 打赏
  • 举报
回复
谢谢大家了~~
cnming 2009-04-21
  • 打赏
  • 举报
回复
写个线程死循环,判断到了这个时间就启动,应该会提高精度,如果当心资源开销问题,可以使用定时器逼近定时点之后展开死循环判定定时时间
llsen 2009-04-21
  • 打赏
  • 举报
回复
学习一楼
LemIST 2009-04-21
  • 打赏
  • 举报
回复
Timer控件
cppfaq 2009-04-21
  • 打赏
  • 举报
回复
只能用P/Invoke了,.net BCL提供的定时器精度都达不到要求
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

public class MMTimer : IDisposable {
//Lib API declarations
[DllImport("Winmm.dll", CharSet = CharSet.Auto)]
static extern uint timeSetEvent(uint uDelay, uint uResolution, TimerCallback lpTimeProc, UIntPtr dwUser,
uint fuEvent);

[DllImport("Winmm.dll", CharSet = CharSet.Auto)]
static extern uint timeKillEvent(uint uTimerID);

[DllImport("Winmm.dll", CharSet = CharSet.Auto)]
static extern uint timeGetTime();

[DllImport("Winmm.dll", CharSet = CharSet.Auto)]
static extern uint timeBeginPeriod(uint uPeriod);

[DllImport("Winmm.dll", CharSet = CharSet.Auto)]
static extern uint timeEndPeriod(uint uPeriod);

//Timer type definitions
[Flags]
public enum fuEvent : uint {
TIME_ONESHOT = 0, //Event occurs once, after uDelay milliseconds.
TIME_PERIODIC = 1,
TIME_CALLBACK_FUNCTION = 0x0000, /* callback is function */
//TIME_CALLBACK_EVENT_SET = 0x0010, /* callback is event - use SetEvent */
//TIME_CALLBACK_EVENT_PULSE = 0x0020 /* callback is event - use PulseEvent */
}

//Delegate definition for the API callback
delegate void TimerCallback(uint uTimerID, uint uMsg, UIntPtr dwUser, UIntPtr dw1, UIntPtr dw2);

//IDisposable code
private bool disposed = false;

public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}

private void Dispose(bool disposing) {
if (!this.disposed) {
if (disposing) {
Stop();
}
}
disposed = true;
}

~MMTimer() {
Dispose(false);
}

/// <summary>
/// The current timer instance ID
/// </summary>
uint id = 0;

/// <summary>
/// The callback used by the the API
/// </summary>
TimerCallback thisCB;

/// <summary>
/// The timer elapsed event
/// </summary>
public event EventHandler Timer;

protected virtual void OnTimer(EventArgs e) {
if (Timer != null) {
Timer(this, e);
}
}

public MMTimer() {
//Initialize the API callback
thisCB = CBFunc;
}

/// <summary>
/// Stop the current timer instance (if any)
/// </summary>
public void Stop() {
lock (this) {
if (id != 0) {
timeKillEvent(id);
Debug.WriteLine("MMTimer " + id.ToString() + " stopped");
id = 0;
}
}
}

/// <summary>
/// Start a timer instance
/// </summary>
/// <param name="ms">Timer interval in milliseconds</param>
/// <param name="repeat">If true sets a repetitive event, otherwise sets a one-shot</param>
public void Start(uint ms, bool repeat) {
//Kill any existing timer
Stop();

//Set the timer type flags
fuEvent f = fuEvent.TIME_CALLBACK_FUNCTION | (repeat ? fuEvent.TIME_PERIODIC : fuEvent.TIME_ONESHOT);

lock (this) {
id = timeSetEvent(ms, 0, thisCB, UIntPtr.Zero, (uint) f);
if (id == 0) {
throw new Exception("timeSetEvent error");
}
Debug.WriteLine("MMTimer " + id.ToString() + " started");
}
}

void CBFunc(uint uTimerID, uint uMsg, UIntPtr dwUser, UIntPtr dw1, UIntPtr dw2) {
//Callback from the MMTimer API that fires the Timer event. Note we are in a different thread here
OnTimer(new EventArgs());
}
}

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧