Is there a way to execute a method in high resolution frequency in C#, e.g. every 100 microseconds?
What about two different methods at different frequencies, e.g. Method1 every 0.1 ms and Method2 0.3 ms?
MSDN has documentation on attaching an event handler to the Systems.Timers.Timer
class like this:
public static void Main()
{
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(Method);
aTimer.Interval = 2000;
aTimer.Enabled = true;
GC.KeepAlive(aTimer);
}
But System.Timers.Timer
only has a 15~16 ms precision when I benchmarked it. My CPU's performance counter has 0.1 ms precision and below, but I cannot find a way to attach an event handler to the Stopwatch
class.
Also, is there some threading issue that I need to be aware of when I am executing the two methods at different frequencies?
Thanks!