本文介绍了c# -- 如何制作微秒计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以帮我吗?
如何在 c# 中制作微秒计时器?
和其他计时器一样,我想在计时器主体中做点什么.
Can anyone help me?
How can I make a microsecond timer in c#?
Like other timers, I want to do Something in the timer body.
推荐答案
这里可以下载MicroLibrary.cs:http://www.codeproject.com/Articles/98346/Microsecond-and-Millisecond-NET-Timer
您的问题示例:
Here you can download MicroLibrary.cs:http://www.codeproject.com/Articles/98346/Microsecond-and-Millisecond-NET-Timer
Example for your problem:
private int counter = 0;
static void Main(string[] args)
{
Program program = new Program();
program.MicroTimerTest();
}
private void MicroTimerTest()
{
MicroLibrary.MicroTimer microTimer = new MicroLibrary.MicroTimer();
microTimer.MicroTimerElapsed +=
new MicroLibrary.MicroTimer.MicroTimerElapsedEventHandler(OnTimedEvent);
microTimer.Interval = 1000; // Call micro timer every 1000µs (1ms)
microTimer.Enabled = true; // Start timer
System.Threading.Thread.Sleep(2000); //do smth 2 seconds
microTimer.Enabled = false; // Stop timer (executes asynchronously)
Console.ReadLine();
}
private void OnTimedEvent(object sender,
MicroLibrary.MicroTimerEventArgs timerEventArgs)
{
// Do something every ms
Console.WriteLine(++counter);
}
}
}
这篇关于c# -- 如何制作微秒计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!