I inherited a form that did a lot of processing during the load event and I had a problem with displaying a mostly blank form...
Me.BeginInvoke(New MethodInvoker(AddressOf Me.Refresh)) 'this will show the screen, then
Me.BeginInvoke(New MethodInvoker(AddressOf OpenFile)) 'this will do a lot of work
End If
End If
End Sub
Using BeginInvoke allows the load event to finish, then do the Screen refresh showing something besides a lot of white space.
The second BeginInvoke allows the Refresh to finish before starting the long work of processing a data file.
Another approach would be to load the file on a worker thread and sync things up when done, but since the user can't do anything until the file is loaded a simple progress bar was sufficient here.
Another combination that appears to work that I somehow missed* before going to BeginInvoke is this:
Me.Visible = True
End If
End Sub
*I find it hard to believe that Refresh is not the first thing I tried. I seem to recall it just wouldn't work with something horrible like a DoEvents. Which was fatal in this app because the legacy code would start processing mouse-moves trying to use the data that hasn't been loaded yet. There is also a progress meter that needs to refresh during the load (which works just fine), although there was a textbox that would not -- both on a toolstrip. I believe the progress uses its parent control to refresh, I'll have to look at the textbox again.
Does Refresh always work?
I recall instances in the past where I couldn't succeed with Refresh, but so far I can't find an example. I redid the project above that prompted this investigation with Refresh and it worked this time. I don't see why it shouldn't work, here is Refresh()
Public Overridable Sub Refresh() |
Update is just a wrapper around UpdateWindow. I would say Invalidate() puts a WM_PAINT in the queue for each control, then Update() scans the queue and sends the WM_PAINT to the control. From MSDN (makes it sound a little different than what I am saying but I think they are simplifying for clarity):
"The UpdateWindow function updates the client area of the specified window by sending a WM_PAINT message to the window if the window's update region is not empty. The function sends a WM_PAINT message directly to the window procedure of the specified window, bypassing the application queue. If the update region is empty, no message is sent."
Sounds good. Under what conditions wouldn't this paint the whole form? Maybe my previous problems were actually bad custom controls or some-other non-Windows issue and this whole entry was wasted on a non-problem.
What's old is new again
However, check this out:
http://geekswithblogs.net/NewThingsILearned/archive/2008/08/25/refresh--update-wpf-controls.aspx
Also, Hide-and-see
When hiding a form and then re-showing it, e.g., Show() or Visible=True; is a Refresh needed there, too? It would seem so.
No comments:
Post a Comment