Force wait for Invalidate to finish??

aewarnick

Well-known member
Joined
Jan 29, 2003
Messages
1,031
How can I force my program to wait right where it is at for Invalidate to finish its code?

Here is my problem
C#:
public bool SaveSurface(string path)
{
	this.save= true;
	this.SavePath= path;
	this.Invalidate();
	return saved;
}
There is a bool value set in OnPaint that indicates whether the picture was saved or not.

The problem is that Invalidate is called and does not wait to finish but immediately returns a false value that should be true because it was not set in OnPaint yet.
 
Invalidate executes the OnPaint method during screen refresh. There is no way to make it run faster without calling it directly (to my knowledge).

Why would you need OnPaint to run, anyway?

I guess Application.DoEvents might help, but Id strongly recommend you look into a different approach.

-- Pete
 
I think you are getting the wrong impression of what I am doing. There is a bool value that is set in OnPaint where I save my image to disk. The bool value indictates whether the save was successful or not.

With the code above, Invalidate is called and the program does not stop and wait there, it just returns the saved bool value and it is a wrong value because it was sent before OnPaint finished.

I need a way to make the program wait till OnPaint is finished. I could use a loop and exit it only when another bool value at the end of OnPaint is reached indicating that the loop in SaveSurface can exit.

But if there is a better way that someone knows of, Id like to know about it.
 
How about not saving anything to disk in the OnPaint override? Its only really meant for screen painting you know. If you need a Graphics object just to save to disk you could use CreateGraphics to get one.

If you have common code between OnPaint and the image you want to save, just use a method and pass a graphics object to it. In OnPaint pass e.Graphics, for saving pass newly created graphics object (dont forget to dispose when method returns).

...Or maybe Im still not understanding the problem? Are you trying to take a screenshot or something? In which case this might help:

http://www.syncfusion.com/faq/winforms/search/625.asp

-- Pete
 
Yes, now you understand, and the code in OnPaint is common to the Bitmap I am saving, so it needs to be linked to OnPaint somehow at least.

I never considered yet, changing my code around. I thought there would be some way to pause the program and wait till OnPaint finishes. And I still can; using a loop.

Here is the scenerio:
I have a form with a control on it called a DrawingSurface (my own control). The DrawingSurface does everything including saving the image to disk. The form calls a method in DrawingSurface - SaveSurface (Code at top of thread). SaveSurface needs to return saved (true false) AFTER OnPaint has finished.

Do you see where the problem is? Invalidate() is called and immediately returns saved without waiting for Invalidate to finish. So, you see, the problem is not saving the image, it is returning to the form whether the image was save successfully or not.
 
If I understand correctly, this may help. Create a boolean variable
thats set to false before calling Invalidate. Create an
OnInvalidated method (or a handler for the Invalidated event),
and then set the boolean value to true. In the code where you
need to wait for the invalidate to complete, make a little loop that
exits when the boolean variable is set to true, and stick an
Application.DoEvents() call in the loop to give it time to process.
 
Yes, earlier in the thread I had mentioned everything except the Application.DoEvents() call.

What does that do?
 
When your code is running, nothing else in the system is. Your
app is using up all of the processors time, for however short or
long. A call to DoEvents() stops your code for a split-second and
allows Windows to process other messages, which in this case
would be the message to redraw your control. By putting it in a
loop, the code in the Invalidated event is given sufficient time to
execute, and your other code will only continue when the
boolean value is set to true.

[mshelp=ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfSystemWindowsFormsApplicationClassDoEventsTopic.htm]Read more![/mshelp]
 
Thank you both. I learned something useful today! So, in the loop I should put a check for the bool value AND the Application.DoEvents() call?
 
Originally posted by aewarnick
Here is the scenerio:
I have a form with a control on it called a DrawingSurface (my own control). The DrawingSurface does everything including saving the image to disk. The form calls a method in DrawingSurface - SaveSurface (Code at top of thread). SaveSurface needs to return saved (true false) AFTER OnPaint has finished.

So is there any particular reason that it has to be done this way? Whats wrong with just using a common method to do the drawing and calling it separately from SaveSurface and OnPaint with different graphics objects?

-- Pete
 
Thanks for asking. It has caused me to look at my code more intently only to find that there is nothing in my code that requires it to be in OnPaint!!

Ill move it out of there but at least I have learned something new! Application.DoEvents().

By the way, that did work perfectly. Thank you both!
 
Back
Top