There's a section in the FAQ which goes into detail on this topic: How do I keep my program from flickering?
Thank you!