   MDX1.1 - having trouble controling FPS
  Hello.I'm writing a 2d spaceship shooter game (like subspace continuum),and while doing so, have encountered some issues with fps.I want to be able to completely control the fps in the game, from 1, 5 to 100.I use this timer class:public class MyTimer    {   #region Constructor   public MyTimer()   {   }   #endregion   #region Initialization   public void Init(float seconds)   {  m_SecondsToCount = seconds;   }   #endregion   #region Public Methods   public void Start()   {  m_LastTick = Environment.TickCount;   }   public bool IsPassed()   {  float diff = Environment.TickCount - m_LastTick;  if (diff / 1000 >= m_SecondsToCount) return true;  else return false;   }   public int MilisecondsLeft()   {  int result = (int)(m_SecondsToCount * 1000 -    (Environment.TickCount - m_LastTick));  return result < 0 ? 0 : result;   }   public int MiliSecondsLeftUnbounded()   {  return (int)(m_SecondsToCount * 1000 -  (Environment.TickCount - m_LastTick));   }   public int ElapsedMiliSeconds()   {  return Environment.TickCount - m_LastTick;   }   #endregion   #region Properties   public float SecondsToCount   {  get { return m_SecondsToCount; }   }   #endregion   #region Fields   private int m_LastTick;   private float m_SecondsToCount;   #endregion    }Here is my paint methodprotected override void OnPaint(PaintEventArgs e)   {  m_FrameTimer.Init(1.0f / m_FramesPerSecond);  m_FrameTimer.Start();  D3DDevice.Clear(Color.Black);  D3DDevice.Device.BeginScene();  Background.Draw(D3DDevice.Device);  foreach (Player player in GameEngine.Players) player.Ship.Draw(D3DDevice.Device);  D3DDevice.Device.EndScene();  D3DDevice.Device.Present();  this.Invalidate();  Thread.Sleep(m_FrameTimer.MilisecondsLeft());  int ml = m_FrameTimer.MiliSecondsLeftUnbounded();   }Initially i set m_FramesPerSecond = 50.I measured the amount of time it takes to render a frame(before the Thread.Sleep call) and its zero in miliseconds or very close to it,So almost always i call Thread.Sleep with 20;Therefore, i expect to get 50 FPS.however, after the sleep, ml equals -11, which is nearly 0.01 seconds.thats quit a lag, and results in 32 FPS only.Strangly enough, it seems that m_FramesPerSecond has no great influence onthe lag. Values between 65 - 1000 results in 64 FPS, 65 - 32 results in 32, 1 - 32 results in somewaht accurate FPS, but not quite, the FPS is usuallylower by 4-6 frames than m_FramesPerSecond.Ok, so maybe the Thread.sleep method is not so accurate.I replaced it with this:while(!m_FrameTimer.IsPassed());Made no difference! :(Besides the drawing thread, i have 2 more threads, 1 for catching keyboardevents, and 1 for moving and rotating the ships. I thought it might be somehow related to them, race conditions and all, so i disabled them.Sadly, that didnt help either.If i Erase the Thread.Sleep call, i get lots and lots of frames, around 1500,which makes my video card quickly warmed up. Thats one of the reasons whyi want full control over FPS.Another problem that i have, which i think is related to this, are glitches.About once a sec, or 2 seconds, i get a nasty glitch on the screen. Itsnot so big, but its defently noticable. I'm running my monitor in 85 hertz,so i think if i manage to set my FPS to 85, it will eliminate the glitches.am i right? but ofcourse i cant set my FPS to 85 because of the first wierd problem.I'll be glad for any insights anyone have on this.thanks in advance.   