Bernt

Bernt

  • NA
  • 10
  • 0

Interrupt latency

May 21 2010 3:58 PM

I am making a threaded application with real time audio processing. Latency is a no-no. Below is some test code that exposes an ugly problem:

[
DllImport("winmm.dll")]
internal static extern uint timeBeginPeriod(uint period);
[
DllImport("winmm.dll")]
internal static extern uint timeEndPeriod(uint period);
timeBeginPeriod(1);
System.Threading.
AutoResetEvent Pause = new System.Threading.AutoResetEvent(false);
SWatch.Reset();
SWatch.Start();
for (int i = 0; i < 1000; i++)
Pause.WaitOne(1);
SWatch.Stop();
Duration = SWatch.ElapsedMilliseconds;
timeEndPeriod(1);


 
The total duration is about 15 seconds. It should have been very close to 1 second. This means ca 15 milliseconds latency for the Autoresetevent to respond.  Using winmm.dll helps on thread.sleep(1) but not here. I was originally planning to use a manualresetevent to stall a thread until it was time for another loop. Ideally I should have microsecond latency.  When it takes 15 ms to respond, I need to use really huge buffers and thus I get far from real time performance.

My question is: Is there a way to pause and restart (by event) a thread - or start a new thread - with low microseconds latency from C#? Since basically anything can be called by using interop and similar, I guess I could add: What is the fastest way to restart a paused thread on Windows XP, Vista, W7 - inside or outside .NET?