帮忙看看这个简单的类调用

vachul 2011-04-22 03:26:49

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;


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);
Console.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");
Console.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());
}
}


这个类在winform里面怎么调用啊??高手们!
...全文
131 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
vachul 2011-04-22
  • 打赏
  • 举报
回复
这个MMTimer能精确到10毫秒吗?
vachul 2011-04-22
  • 打赏
  • 举报
回复
因为我这个需要精确到毫秒,所以用Thread.sleep会有误差
现在我就像试下这个能不能精确到10毫秒左右
我姓区不姓区 2011-04-22
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 vachul 的回复:]

引用 11 楼 ojlovecd 的回复:
你是要干嘛啊?

延时1s啊,这1s里面什么都不做
[/Quote]
那就直接Thread.Sleep(1000);就好啦
这个MMTimer类只是另开一个线程作为计时器而已,跟你这个需求有什么关系?
vachul 2011-04-22
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 ojlovecd 的回复:]
你是要干嘛啊?
[/Quote]
延时1s啊,这1s里面什么都不做
我姓区不姓区 2011-04-22
  • 打赏
  • 举报
回复
你是要干嘛啊?
vachul 2011-04-22
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ojlovecd 的回复:]
C# code

static void Main(string[] args)
{
MMTimer timer = new MMTimer();
timer.Timer += new EventHandler(timer_Timer);
timer.Start(1000, true……
[/Quote]

什么都不做,延迟1s是怎样写啊?
我姓区不姓区 2011-04-22
  • 打赏
  • 举报
回复

static void Main(string[] args)
{
MMTimer timer = new MMTimer();
timer.Timer += new EventHandler(timer_Timer);
timer.Start(1000, true);
Console.Read();
}

static void timer_Timer(object sender, EventArgs e)
{
Console.WriteLine("ojlovecd");
}

vachul 2011-04-22
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 etudiant6666 的回复:]
start()以后应该是在另一个线程执行某些工作,并使当前线程处于停顿状态。 定时器会定时激发事件,DoWhenTimerStart是事件的处理程序,你应该是直接运行就结束了,当然没有什么触发了。在start()后加上个thread.sleep(60000)试试。
[/Quote]
不太行!这样来问吧,怎么调用它,令它的功能可以近似于Thread.sleep(1000);
解决了这个,分就全给你啦
窗户纸 2011-04-22
  • 打赏
  • 举报
回复
start()以后应该是在另一个线程执行某些工作,并使当前线程处于停顿状态。 定时器会定时激发事件,DoWhenTimerStart是事件的处理程序,你应该是直接运行就结束了,当然没有什么触发了。在start()后加上个thread.sleep(60000)试试。
vachul 2011-04-22
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 etudiant6666 的回复:]
void DoWhenTimerStart(object sender, EventArgs e)
[/Quote]
这个类的意思是不是每隔1s就执行这个DoWhenTimerStart方法啊?
我刚试运行了下,发现它没执行这个DoWhenTimerStart!
你那句mytimer.Timer+= new EventHandler(DoWhenTimerStart);
是神马意思?
窗户纸 2011-04-22
  • 打赏
  • 举报
回复
void DoWhenTimerStart(object sender, EventArgs e)
vachul 2011-04-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 etudiant6666 的回复:]
MMTimer mytimer =new MMTimer();
mytimer.Timer+= new EventHandler(DoWhenTimerStart);
mytimer.start(1000, true );

//....
mytimer.stop();


void DoWhenTimerStart(...)
{
}
[/Quote]

1 No overload for 'DoWhenTimerStart' matches delegate 'System.EventHandler'
窗户纸 2011-04-22
  • 打赏
  • 举报
回复
MMTimer mytimer =new MMTimer();
mytimer.Timer+= new EventHandler(DoWhenTimerStart);
mytimer.start(1000, true );

//....
mytimer.stop();


void DoWhenTimerStart(...)
{
}
vachul 2011-04-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bdmh 的回复:]
MMTimer t = new MMTimer();
[/Quote]
我自己已经写了这样的:
MMTimer mt = new MMTimer();
mt.Start(100, false);
但是还是有问题,你帮我试下吧
bdmh 2011-04-22
  • 打赏
  • 举报
回复
MMTimer t = new MMTimer();

110,538

社区成员

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

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

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