private void CompletionThreadProc() { Utility.SetCurrentThreadAffinity(); while (true) { long wait; Completion completion = null; lock (this) { if (_completions.Count > 0) { completion = _completions[0]; wait = (long)(completion._ticks - this.CurrentTicks); if (wait <= 0) { _completions.RemoveAt(0); } else { completion = null; //turn wait from ticks to msec wait = wait / TimeSpan.TicksPerMillisecond; if (this.IsTimeSuspended) { //No need to wake up until time resumes. wait = Timeout.Infinite; } } } else { wait = Timeout.Infinite; } } if (completion != null) { //Is this ok outside the lock? //Emulator completions can be called back completion.OnCompletion(); } else { if (WaitHandle.WaitAny(_waitHandles, (int)wait, false) == c_WaitHandleShutdown) { break; } } } }