PC Review


Reply
Thread Tools Rate Thread

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

 
 
raylopez99
Guest
Posts: n/a
 
      6th Jul 2008
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
}


}
 
Reply With Quote
 
 
 
 
Ben Voigt [C++ MVP]
Guest
Posts: n/a
 
      6th Jul 2008


"raylopez99" <(E-Mail Removed)> wrote in message
news:a6aa7b29-fa8e-439c-8201-(E-Mail Removed)...
> 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?

>
> // 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
> }
>
>
> }


 
Reply With Quote
 
raylopez99
Guest
Posts: n/a
 
      7th Jul 2008
On Jul 6, 3:55*pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospam> wrote:

> 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!");
}
 
Reply With Quote
 
Ciaran O''Donnell
Guest
Posts: n/a
 
      7th Jul 2008
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?
--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com


"raylopez99" wrote:

> On Jul 6, 3:55 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospam> wrote:
>
> > 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!");
> }
>

 
Reply With Quote
 
raylopez99
Guest
Posts: n/a
 
      8th Jul 2008
On Jul 7, 6:11*am, Ciaran O''Donnell
<CiaranODonn...@discussions.microsoft.com> wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
break out of continuous for loop in key down event? Rich P Microsoft C# .NET 10 3rd Nov 2009 10:48 PM
Detect page break preview ward376 Microsoft Excel Programming 2 24th Dec 2007 03:31 PM
Detect page break settings =?Utf-8?B?SlJfMDYwNjIwMDU=?= Microsoft Excel Worksheet Functions 0 26th Jan 2006 02:20 PM
detect ctrl break to end process =?Utf-8?B?bGFzdHVzZXJuYW1lbGVmdA==?= Microsoft Dot NET 0 21st Jun 2005 05:31 PM
Custom Event handling + thread handling. Michael McCarthy Microsoft C# .NET 1 14th Jun 2005 02:50 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:53 AM.