Refresh problem on a simple Win Form

  • Thread starter Thread starter Emanuele Ornella
  • Start date Start date
E

Emanuele Ornella

Hello everybody,
I'm just trying to do a simple task: display a count number that
increases in the time in a label on a Form.
So I uses the label1.Refresh( ) method to force the drawinig:

private void counting()
{
for (int i = 0; i < 100;i++ )
{
System.Threading.Thread.Sleep(100);
label1.Text = "Counting ... " + i;
label1.Refresh();
}
MessageBox.Show("END");
}

That works fine if I do not move the Form. If I try to move or resize
the form I have the label "frozen" on the number displayed when the
movement started.
So I tryed to refresh the Form too inside the loop:

private void counting()
{
for (int i = 0; i < 100;i++ )
{
System.Threading.Thread.Sleep(100);
label1.Text = "Counting ... " + i;
label1.Refresh();

this.Refresh();

}
MessageBox.Show("END");
}

But I have the same result.

Any suggestion?
Thanks
ema
 
Emanuele,

First of all if you do the counting in the main UI thread you shouldn't be
able to move or resize the form while counting. The reason being is that you
stop the message loop from pumping messages to the application. Playing with
your code I found that at some point I can start moving the application,
which freezes the updating until the loop finishes. I don't think this is a
normal behavior and there is probably something happening during the label
refresh. It might have some explanation, but I'd rather consider this as
some side effect of something that the label control does when updating
itself.

To keep the application responsive instead of label1.Refresh() call
Application.DoEvents(). This, as I said, will keep the application
responsive and update the label at the same time.

When you start moving the form the application enters a
modal loop and doesn't process other messages until the dragging stops.
That's why the label is not updated while moving. This is done in response
to WM_ENTERSIZEMOVE message. You can find more info about this message in
MSDN.
The application enters the modal loop in the DefWindowProc and doesn't exit
until the mouse is released and the position or size is fixed. For you it
means that not only the update, but the counting will stop while the form
is being moved or resized.
BTW the application enters modal loop also when a menu is pulled down so you
are going to have the same problem there.

In order to at least keep counting you need to run the counting method in a
separate worker thread.
 
Stoitcho said:
stop the message loop from pumping messages to the application. Playing with
your code I found that at some point I can start moving the application,
which freezes the updating until the loop finishes. I don't think this is a
normal behavior and there is probably something happening during the label

This is a feature of WindowsXP, I believe. It creates a pseudo-window
that allows you to move the app around, even if it appears to be locked
up.
 
Thanks!
I also tried to use threads. My goal is to have a normal progress bar
while I'm polling clients in LAN and scan some files there.
But as soon as I found so many problems to refresh UI because of the
threading and problem accessing UI in a different thread, I try to use
something easier...

It's really fun that it is so complicated to have a progress bar
working using a RAD as Visual Studio...

Anyway I will quite because I have spent too much time in this not key
task of my small application.

Thanks a lot for your reply,
ema
 
Emanuele,

Use BackgroundWorker component. It's specially designed for implementing
progress indicators as well as it marshals the events you need to the main
UI thread so you don't need to worry about updating the UI.
 
Chris,

I haven't heard about that and forgive me, but I really doubt it's true. Can
you point me to some information on this.
 
Thanks a lot!
There is a complete example using Fibonacci numbers on the MSDN
(ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref3/html/T_System_ComponentModel_BackgroundWorker.htm)
about this asyncronous background progress bar..

That's fantastic! I wonder why I didn't find anything on the net about
this. Maybe it's because it's a new feature of .NET 2 ??
Anyway it's excalty what I was looking for.

Thanks again!
ema



Stoitcho Goutsev (100) ha scritto:
 
Back
Top