I have a form with a panel in which I want to display an animated 3D scene using XNA. What’s the best way to have this panel continuously repainted?
I’m currently thinking about adding a System.Windows.Forms.Timer to the form and adding an event handler to Timer.Tick which updates my 3D scene and calls Invalidate on the panel.
Is there a better way to do this? The rendering isn’t critical, I don’t want the rest of the form to become inresponsive, but on the other hand if there’s time available I would like the rendering to happen at the refresh rate of the monitor.
Yes, a Timer would work. Here’s a clever trick I saw used in a Microsoft example that showed how to use XNA in a Winforms project. It uses the Application.Idle event:
I used a PictureBox instead of a Panel to hide the flicker, PB has the DoubleBuffered property turned on. That isn’t very relevant to XNA though. Using vsync isn’t going to work afaik. You probably ought to check out that example, the ‘spinning triangle’ was completely smooth without any tearing when I tried it. This thread talks about it.