main page >programming tips (VB5/6) > upgraded Refresh method diese Seite auf deutsch | |
With a time-consuming procedure that is supposed to display its progress, you have to explicitly give Windows a chance to draw that progress display.
Example:
Running this code you will quickly notice that the Label doesn't display the current iteration at all. It works, however, with the following addition:
Instead of Label1.Refresh one could also use the DoEvents command;
however, this would allow execution of all
events, such as another click on the button which would start the routine for a second time (reentrancy).
Hence precautions would be required to prevent this from happening.
With .Refresh, you don't need any precautions.
The downside is that with Windows XP and later, this only works for a short time span; after a few seconds of being unresponsive, Windows assumes that the program is too busy to redraw itself, and thus creates a screenshot which is then displayed above the Form until the program responds again. While this may be nice in some situations, it ruins our efforts to dynamically display something on the screen. Now we could use DoEvents again to solve the problem, but as discussed above there are good reasons to avoid this. Here comes a solution that replaces the Refresh method without any unwanted side effects:
The parameter passed to this method - which, by the way, works on all Win32 versions before and after
XP - is the window handle (hWnd) of the control or form to be redrawn.
Since a label is windowless and hence has no window handle, we pass the handle of the owning form instead:
|
|
main page > programming tips (VB5/6) > this page | |
© 2001-2021 Wolfgang Enzinger |