4
votes
  1. Is there a way to execute a method in high resolution frequency in C#, e.g. every 100 microseconds?

  2. 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!

3
You should look at systems with realtime OSes if you need that sort of precision. en.wikipedia.org/wiki/Real-time_operating_systemtvanfosson
I agree with @tvanfosson, with time periods like this you are mrew likely to be impacted by stuff like multitaskingNot loved
@AYK: I have a remotely-guided vehicle that is sending information to my workstation at several kHz, so processing time has to be comparable so it doesn't cause a bottleneck.elleciel
@ephedyn - have you thought about running your task slower and interpolating? is the vehicle moving quickly enough for the values to change significantly in 20+ ms?tvanfosson
You might be able to get the resolution you want using interop to the Win32 multimedia timers (if they haven't been added to the .NET framework yet). Here's an old article that outlines one possible solution: codeproject.com/Articles/5501/…Michael Burr

3 Answers

3
votes

It is ugly, but you may have some chance using the stopwatch ElapsedTicks.

Stopwatch w = new Stopwatch()
long nextTick = 0;
long ticksPerMicroSecond = 1000000L / Stopwatch.Frequency; // Number of ticks per microsec.
long periodInTicks = ticksPerMicroSecond / 100; // Ticks per 0.1ms

while(true)
{
  long currentTick = w.ElapsedTicks.
  if (currentTick  > nextTick)
  {
    nextTick = currentTick + periodInTicks;
    // Do something here.
  }
}

It is as ugly as it can get, and you get a while(true) loop, that will probably be using 100% cpu for free (better run this on a multi core, then).

You may want to benchmark the thing a bit (look at examples in Stopwatch.Frequency msdn doc), to see how precise your timer can be, and see the drift you get from being late with your inner function. You may also want to enable background gc to avoid GC blocking your thread. While this shows it is dable in code, I'm really not sure you can get your function run in less than 0.1ms.

2
votes

This is not possible. Windows is not a real time operating system.

-2
votes

Not possible. You are below what MS develoeprs considered reasonable.

The best yo ucan do- asssuming modern hardware - is TImerQueue, HPET. Can be disabled in bios, so tweaking there may be needed.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms686796(v=vs.85).aspx

No .NET API available, but this is trivial to map via Interop.

THAT SAID: the timing parameter there has a int period and is given in millisecond, so we talk of a 1ms resolution only. And whether the hardware does that depends MOSTLY on age.

So, sorry, that is about the best you can get.