void RunInternal() { if (m_running == true) { throw new Exception("Scheduler already running"); } if (m_scheduledThreadCount == 0) { return; } m_running = true; m_continuation.Mark(); // status 1 = new thread to be started, m_currentThread has been set // status 2 = exiting int status = m_continuation.Store(1); if (status == 1) { // status 1 = new thread to be started, m_currentThread has been set if (m_currentThread.m_state != MicroThreadState.Starting) { throw new Exception(String.Format("illegal state {0}", m_currentThread.m_state)); } try { Print("Starting new thread {0}", m_currentThread); m_currentThread.m_state = MicroThreadState.Running; #if MT_TIMING m_stopWatch.Reset(); m_stopWatch.Start(); #endif m_currentThread.Run(); #if MT_TIMING m_stopWatch.Stop(); m_currentThread.m_ticks += m_stopWatch.ElapsedTicks; #endif // When we are here the thread has finished Print("Thread {0} finished", m_currentThread); } catch (Exception e) { Console.WriteLine("Unhandled Exception in thread {0}", m_currentThread); Console.WriteLine("Thread terminated"); Console.WriteLine(e.ToString()); } m_currentThread.m_state = MicroThreadState.Stopped; m_currentThread.Dispose(); RemoveCurrentThread(); ScheduleNext(); // Never reached throw new Exception(); } else if (status == 2) { m_currentThread = null; m_previousThread = null; m_scheduledThreadCount = 0; m_waitingThreadCount = 0; Print("Scheduler exiting"); return; } else { throw new Exception("Urrgh illegal restore status"); } // never reached //throw new Exception(); }