title: Precision timing in .NET
body: <p>I've just seen <a href="https://stackoverflow.com/questions/795377/what-if-any-is-the-resource-penalty-for-using-system-diagnostics-stopwatch">this question</a>, where one of the answers indicates that <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch(loband).aspx" rel="nofollow noreferrer">System.Diagnostics.Stopwatch</a> should only be used for diagnosing performance and not in production code.</p>
  
  <p>In that case, what would be the best way to get precision timing in .NET? I'm currently in the early stages of building a very simple <a href="http://en.wikipedia.org/wiki/Musical_Instrument_Digital_Interface" rel="nofollow noreferrer">MIDI</a> sequencer using the MIDI-out functionality of <a href="http://www.codeplex.com/naudio" rel="nofollow noreferrer">NAudio</a>. I'd like to be able to send MIDI messages out aligned to (say) 1/10s with as little jitter as possible. Is this feasible, or will things like context-switching ruin my day?</p>
  
  <p>I currently have some code in a console app that continuously calls <code>Stopwatch</code> and calculates the jitter when generating a stream of 1/16th-notes at 150bpm. The jitter is very low in this situation. However, I'll be moving this off to another thread, so I don't know if that will remain the case.</p>