Reputation: 11
I have a WPF application that controls a robot arm. It's also a human-in-the-loop system. So I would like it to be as close to real time as possible. I am using a DispatcherTimer to generate a "tick" regularly to update the robot arm. Sort of like a poor mans Gameloop:
public partial class MainWindow : Window
{
DispatcherTimer Timer;
public MainWindow()
{
InitializeComponent();
Timer = new DispatcherTimer(DispatcherPriority.Send);
Timer.Interval = TimeSpan.FromMilliseconds(10);
Timer.Tick += new EventHandler(OnTick);
}
private void OnTick(object sender, EventArgs e)
{
//1.Read input devices
//2.Do calculations
//3.Move robot arm
}
}
To be as near to real time as possible, this tick has to be generated as fast as possible, but because of limitations in Windows, the line...
Timer.Interval = TimeSpan.FromMilliseconds(10);
...has no real effect for values under ~15. It is limited by the fact that Windows schedules the "sleeps" and "wakes" of threads in 15.6ms intervals as explained nicely by Bruce Dawson here.
But, as is also explained in this article, this default scheduling interval can be changed by calling timeBeginPeriod in timeapi.h
Can someone show or point me to an example how to call WinAPI functions, like timeBeginPeriod(10);
from a WPF application?
Upvotes: 0
Views: 142