|
using System;
namespace MicroTimerConsoleDemo
{
class Program
{
static void Main(string[] args)
{
Program program = new Program();
program.MicroTimerTest();
}
private void MicroTimerTest()
{
// Instantiate new MicroTimer and add event handler
MicroLibrary.MicroTimer microTimer = new MicroLibrary.MicroTimer();
microTimer.MicroTimerElapsed +=
new MicroLibrary.MicroTimer.MicroTimerElapsedEventHandler(OnTimedEvent);
microTimer.Interval = 1000; // Call micro timer every 1000µs (1ms)
// Can choose to ignore event if late by Xµs (by default will try to catch up)
// microTimer.IgnoreEventIfLateBy = 500; // 500µs (0.5ms)
microTimer.Enabled = true; // Start timer
// Do something whilst events happening, for demo sleep 2000ms (2sec)
System.Threading.Thread.Sleep(2000);
microTimer.Enabled = false; // Stop timer (executes asynchronously)
// Alternatively can choose stop here until current timer event has finished
// microTimer.StopAndWait(); // Stop timer (waits for timer thread to terminate)
// Wait for user input
Console.ReadLine();
}
private void OnTimedEvent(object sender,
MicroLibrary.MicroTimerEventArgs timerEventArgs)
{
// Do something small that takes significantly less time than Interval
Console.WriteLine(string.Format(
"Count = {0:#,0} Timer = {1:#,0} µs, " +
"LateBy = {2:#,0} µs, ExecutionTime = {3:#,0} µs",
timerEventArgs.TimerCount, timerEventArgs.ElapsedMicroseconds,
timerEventArgs.TimerLateBy, timerEventArgs.CallbackFunctionExecutionTime));
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.