• 13
• 18
• 19
• 27
• 10

# [.net] Renderloop causing error on exit

This topic is 3938 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm writing a simple paint application that uses a pictureBox to render output. I have created the following renderloop
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
// Do Drawing stuff here

Application.DoEvents();

pictureBox1.Invalidate();
}
The problem is the Application.DoEvents() line seems to cause the ".NET Framework" error dialog box for a half a second on exit. I can't read what it's reporting because it only flashes up for a brief moment. If I remove the Application.DoEvents() line it seems to go away but I need it otherwise other windows can draw over mine and there are graphical glitches. Anyone know how I can resolve this?

##### Share on other sites
I think you are going about this all the wrong way, and it just so happens that .NET agrees with me and is punishing you for trying to make it jump through a hoop. :)

Here is how I would structure a paint-type program. First, I would have a form that would act as my main drawing area. One thing that many people think is that they have to use a picture box to draw something to the screen. It is really just a waste of time: you might as well draw everything straight to your form, or if you want a little more control just make it a panel. Have the Program class that Visual Studio generates for you call Application.Run using the default form, and then leave the Application class alone.

Second, you should never call Application.DoEvents, Thread.Sleep, or PictureBox.Invalidate inside of an event handler. Calling Application.DoEvents as best is just wasting time, and at worst can throw off your application. Thread.Sleep will cause the entire thread to stall for a short time, which accomplishes nothing for you. And calling PictureBox.Invalidate will just cause the picture box to continue painting itself over and over in an infinite loop. You may be used to having a render loop in a game application where you have complete control of the screen, but when using .NET and WinForms, you should only redraw the screen when it is necessary, and you should never decide when that is. Only .NET knows when it is best to redraw your window (Usually).

When your paint event handler is called, it means that you should redraw the entire output area. To do this, I would keep an internal Image that you perform all your drawing to when using your painting tools, and then copying that to the form during the paint event. Or better yet, use the form's built in BackgroundImage and just draw right onto that. Then you don't have to have a paint event handler at all.

The following is a small proof-of-concept pseudo code sample that should help get you on the right track.
Form1Constructor(){    BackgroundImage = new Bitmap(this.Width, this.Height);    // setting these will make your drawing look smoother and have less flickering    DoubleBuffered = true;    ResizeRedraw = true;}RectangleToolIsUsed(){    // the user wants to draw a rectangle on the paint surface    Graphics graphics = Graphics.FromImage(BackgroundImage);    graphics.DrawRectangle(...);    graphics.Dispose();}

Remember, if you have any more questions or something isn't clear, by all means speak up and ask.

Hope it all helps!