aewarnick Posted September 7, 2003 Posted September 7, 2003 How can I force my program to wait right where it is at for Invalidate to finish it's code? Here is my problem 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. Quote C#
pjv Posted September 7, 2003 Posted September 7, 2003 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 I'd strongly recommend you look into a different approach. -- Pete Quote
aewarnick Posted September 7, 2003 Author Posted September 7, 2003 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, I'd like to know about it. Quote C#
pjv Posted September 8, 2003 Posted September 8, 2003 How about not saving anything to disk in the OnPaint override? It's 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 (don't forget to dispose when method returns). ...Or maybe I'm 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 Quote
aewarnick Posted September 8, 2003 Author Posted September 8, 2003 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. Quote C#
*Experts* Bucky Posted September 8, 2003 *Experts* Posted September 8, 2003 If I understand correctly, this may help. Create a boolean variable that's 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. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
aewarnick Posted September 9, 2003 Author Posted September 9, 2003 Yes, earlier in the thread I had mentioned everything except the Application.DoEvents() call. What does that do? Quote C#
*Experts* Bucky Posted September 9, 2003 *Experts* Posted September 9, 2003 When your code is running, nothing else in the system is. Your app is using up all of the processor's 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] Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
aewarnick Posted September 9, 2003 Author Posted September 9, 2003 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? Quote C#
pjv Posted September 9, 2003 Posted September 9, 2003 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? What's wrong with just using a common method to do the drawing and calling it separately from SaveSurface and OnPaint with different graphics objects? -- Pete Quote
aewarnick Posted September 10, 2003 Author Posted September 10, 2003 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!! I'll 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! Quote C#
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.