private void RenderCallback()
        {
            if (m_messageProcessingStart != MyTimeSpan.Zero)
            {
                MyTimeSpan messageQueueDuration = m_timer.Elapsed - m_messageProcessingStart;
                ProfilerShort.CustomValue("MessageQueue", 0, messageQueueDuration);
            }
            m_waiter.Wait();

            m_frameStart = m_timer.Elapsed;

            ProfilerShort.Begin("PrepareDraw");

            ProfilerShort.Begin("ProcessInvoke");
            Action action;

            while (m_invokeQueue.TryDequeue(out action))
            {
                action();
            }
            ProfilerShort.End();

            ProfilerShort.Begin("ApplyModeChanges");
            ApplySettingsChanges();
            ProfilerShort.End();

            ProfilerShort.Begin("BeforeRender");
            MyRenderStats.Generic.WriteFormat("Available GPU memory: {0} MB", (float)MyRenderProxy.GetAvailableTextureMemory() / 1024 / 1024, MyStatTypeEnum.CurrentValue, 300, 2);
            MyRenderProxy.BeforeRender(m_frameStart);
            ProfilerShort.End();

            ProfilerShort.Begin("RenderWindow.BeforeDraw");
            m_renderWindow.BeforeDraw();
            ProfilerShort.End();

            ProfilerShort.Begin("BeforeDraw(event)");
            var handler = BeforeDraw;

            if (handler != null)
            {
                handler();
            }
            ProfilerShort.End();

            ProfilerShort.End();

            ProfilerShort.Begin("TestCooperativeLevel");
            var deviceResult = MyRenderProxy.TestDeviceCooperativeLevel();

            ProfilerShort.End();

            if (!m_renderWindow.DrawEnabled)
            {
                ProfilerShort.Begin("ProcessMessages");
                MyRenderProxy.ProcessMessages();
                ProfilerShort.End();
            }
            else if (deviceResult == MyRenderDeviceCooperativeLevel.Ok)
            {
                Draw();
            }
            else
            {
                ProfilerShort.Begin("WaitForReset");

                ProfilerShort.Begin("ProcessMessages");
                MyRenderProxy.ProcessMessages();
                ProfilerShort.End();

                if (deviceResult == MyRenderDeviceCooperativeLevel.Lost)
                {
                    ProfilerShort.Begin("DeviceLost");
                    Thread.Sleep(20);
                    ProfilerShort.End();
                }
                else if (deviceResult == MyRenderDeviceCooperativeLevel.NotReset)
                {
                    ProfilerShort.Begin("DeviceReset");
                    Thread.Sleep(20);
                    DeviceReset();
                    ProfilerShort.End();
                }
                else
                {
                    // TODO: OP! Log error code
                }
                ProfilerShort.End();
            }

            ProfilerShort.Begin("AfterRender");
            MyRenderProxy.AfterRender();
            ProfilerShort.End();

            ProfilerShort.Begin("Present");
            if (deviceResult == MyRenderDeviceCooperativeLevel.Ok && m_renderWindow.DrawEnabled)
            {
                this.DoBeforePresent();
                try
                {
                    MyRenderProxy.Present();
                }
                catch (MyDeviceLostException)
                {
                }
                this.DoAfterPresent();
            }
            ProfilerShort.End();

            m_messageProcessingStart = m_timer.Elapsed;
        }