Mono.MicroThreads.Scheduler.SleepInternal C# (CSharp) Method

SleepInternal() static private method

static private SleepInternal ( int milliseconds ) : void
milliseconds int
return void
        void SleepInternal(int milliseconds)
        {
            Print("Putting thread {0} to sleep for {1} ms", m_currentThread, milliseconds);

            if (m_currentThread.m_continuation.Store(0) == 0)
            {
            #if MT_TIMING
                m_stopWatch.Stop();
                m_currentThread.m_ticks += m_stopWatch.ElapsedTicks;
            #endif
                MicroThread thread = m_currentThread;

                RemoveCurrentThread();

                thread.m_state = MicroThreadState.Sleeping;

                DateTime wakeDateTime = DateTime.UtcNow + TimeSpan.FromMilliseconds(milliseconds);
                long wakeTime = wakeDateTime.Ticks;
                thread.m_wakeTime = wakeTime;

                if (m_firstSleepingThread == null)
                {
                    m_firstSleepingThread = thread;
                }
                else if (wakeTime <= m_firstSleepingThread.m_wakeTime)
                {
                    thread.m_next = m_firstSleepingThread;
                    m_firstSleepingThread = thread;
                }
                else
                {
                    MicroThread t = m_firstSleepingThread;

                    while (t.m_next != null && wakeTime >= t.m_next.m_wakeTime)
                        t = t.m_next;

                    thread.m_next = t.m_next;
                    t.m_next = thread;
                }

                m_sleepingThreadCount++;

                ScheduleNext();
            }
            else
            {
                Print("Thread {0} woke up from sleep", m_currentThread);

                if (m_currentThread.m_error != null)
                    throw m_currentThread.m_error;
            }
        }

Usage Example

 public static void Sleep(int milliseconds)
 {
     s_scheduler.SleepInternal(milliseconds);
 }