How to break out of a event handling loop? Detect a minimizedwindow?

R

raylopez99

See comment below. This is a simple problem but I'm a little rusty.
How to break out of a event loop (here _Paint)? I've tried if/else,
case, etc but not quite what I want--I keep getting the JIT system
event exception handler saying "zero area!", or, worse, the Paint loop
refuses to run at all; what I want to do is simply break out of the
_Paint loop, and (optionally) show a MessageBox.

This error happens everytime the "this.ClientRectange" of the window
being painted becomes zero, that is, everytime this window is
minimized.

So one way around this (perhaps the solution to this problem) is to
set up code so that when a window is minimized, _Paint() is not
executed. Or is there a simplier way?

RL

private void Form2MyDialogBox_Paint(object sender, PaintEventArgs e)
{


Rectangle rect = this.ClientRectangle;
int cx = rect.Width;
int cy = rect.Height;
bool TrueOrFalseZeroRect;

if ((cx != 0) || (cy !=0)) { TrueOrFalseZeroRect = false; } else
{ TrueOrFalseZeroRect = true; throw new ArgumentException("zero
area!"); }

// AT THIS POINT IN PAINT, IF THE RECT IS ZERO AREA, I WOULD LIKE TO
BREAK OUT OF PAINT...THE BELOW TRY/CATCH DOESN'T WORK

try
{
// stuff deleted here, not important, basically you get a divide by
zero error when cx=0

}
catch (ArgumentException ex)
{
MessageBox.Show(ex.ToString());
}

finally
{
brush.Dispose(); //clean up brush resources
}


}
 
B

Ben Voigt [C++ MVP]

raylopez99 said:
See comment below. This is a simple problem but I'm a little rusty.
How to break out of a event loop (here _Paint)? I've tried if/else,
case, etc but not quite what I want--I keep getting the JIT system
event exception handler saying "zero area!", or, worse, the Paint loop
refuses to run at all; what I want to do is simply break out of the
_Paint loop, and (optionally) show a MessageBox.

This error happens everytime the "this.ClientRectange" of the window
being painted becomes zero, that is, everytime this window is
minimized.

So one way around this (perhaps the solution to this problem) is to
set up code so that when a window is minimized, _Paint() is not
executed. Or is there a simplier way?

RL

private void Form2MyDialogBox_Paint(object sender, PaintEventArgs e)
{


Rectangle rect = this.ClientRectangle;
int cx = rect.Width;
int cy = rect.Height;
bool TrueOrFalseZeroRect;

if ((cx != 0) || (cy !=0)) { TrueOrFalseZeroRect = false; } else
{ TrueOrFalseZeroRect = true; throw new ArgumentException("zero
area!"); }

Well, you were outside the try/catch when you threw an exception, so it will
hit the OS and do very bad things. Why not show your MessageBox and return
immediately instead of throwing?
 
R

raylopez99

Well, you were outside the try/catch when you threw an exception, so it will
hit the OS and do very bad things.  Why not show your MessageBox and return
immediately instead of throwing?

Thanks Ben Voigt! Indeed, simply placing the 'throw' * inside the
'try' block solved the problem.

And if you comment out the exception handling, it solves the problem
with no distraction to the user (since a minimized window is not
really an error)


RL

* this code:
if (TrueOrFalseZeroRect)
{
// MessageBox.Show("zero_area1!");
throw new ArgumentException("zero area!");
}
 
C

Ciaran O''Donnell

You shouldnt really throw when there are other options, error handling is
expensive. Why not just have this:


if ((cx != 0) || (cy !=0)) {
TrueOrFalseZeroRect = false;
} else {
TrueOrFalseZeroRect = true;
return ;}


Its the better option, surely?
 
R

raylopez99

You shouldnt really throw when there are other options, error handling is
expensive. Why not just have this:

Yes, that's much better; for this example, I was using somebody else's
code and for demonstration purposes I was trying to modify it to work
with try/catch.

RL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top