Sign in to follow this  
Headkaze

[.net] Renderloop causing error on exit

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();

	Thread.Sleep(10);

	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 this post


Link to post
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!

Share this post


Link to post
Share on other sites
I took your advice and removed the renderloop. The thing is when I tried to change it to a panel, it is very slow to redraw. I mean you can see it redraw my grid practically line by line. Double buffering did not fix it either. When I changed it back to a picturebox drawing is smooth again. Perhaps there is a good reason to use a picturebox over a panel? Finally I just added a pictureBox1.Invalidate() when and only when I need to redraw the graphics and it works well. Also without the render loop there is 0 CPU usage (until a redraw but that is brief).

Thanks for the help :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this