Home All Groups Group Topic Archive Search About

Label changes not being reflected until loop is done

Author
14 Aug 2006 6:45 PM
peter.meth
Hi All,

I am making a file manager type of application and am trying to
duplicate Windows Explorer's behaviour when copying files; ie, display
a second form with the copy files animation.  As it is copying files,
it should show the file name and folder.  For testing purposes I am
sleeping for 1 second to simulate a file being copied.  My problem is
that when I run the code, the labels do not change to reflect the file
names until the very end, when only the last filename will be
reflected.  However, if I put a message box before the sleep, the
labels get set as I expected.  I have tried using a timer and a long
loop instead of the sleep, and I have also tried a textbox instead of a
label, but all produce the same behaviour.  Here is the offending code
- it is called by a loop which loops through all the files to be
copied.


Public Sub ProcessFile(ByVal root As String, ByVal pathToFile As
String)
     Dim fi As New FileInfo(pathToFile)
     lblFileBeingProcessed.Text = fi.Name
     lblFromDirectoryToDirectory.Text = "From '" & root & "' to '" &
root & "'"
     'MsgBox("Copy File: " & root & fi.Name)
     System.Threading.Thread.Sleep(1000) ' Sleep for 1 second
     bytesProcessed += fi.Length
     ProgressBar1.Value = Math.Min(100, Math.Round(bytesProcessed /
bytesToProcess * 100))
     myParentForm.ListBox1.Items.Add("Copy File: " & root & fi.Name)
End Sub



Thank you

Author
15 Aug 2006 12:01 AM
GhostInAK
Hello peter.m***@rci.rogers.com,

The label gets updated when the message to repaint it is processed.  So massage
the message pump.  Application.DoEvents.

-Boo

Show quoteHide quote
> Hi All,
>
> I am making a file manager type of application and am trying to
> duplicate Windows Explorer's behaviour when copying files; ie, display
> a second form with the copy files animation.  As it is copying files,
> it should show the file name and folder.  For testing purposes I am
> sleeping for 1 second to simulate a file being copied.  My problem is
> that when I run the code, the labels do not change to reflect the file
> names until the very end, when only the last filename will be
> reflected.  However, if I put a message box before the sleep, the
> labels get set as I expected.  I have tried using a timer and a long
> loop instead of the sleep, and I have also tried a textbox instead of
> a label, but all produce the same behaviour.  Here is the offending
> code - it is called by a loop which loops through all the files to be
> copied.
>
> Public Sub ProcessFile(ByVal root As String, ByVal pathToFile As
> String)
> Dim fi As New FileInfo(pathToFile)
> lblFileBeingProcessed.Text = fi.Name
> lblFromDirectoryToDirectory.Text = "From '" & root & "' to '" &
> root & "'"
> 'MsgBox("Copy File: " & root & fi.Name)
> System.Threading.Thread.Sleep(1000) ' Sleep for 1 second
> bytesProcessed += fi.Length
> ProgressBar1.Value = Math.Min(100, Math.Round(bytesProcessed /
> bytesToProcess * 100))
> myParentForm.ListBox1.Items.Add("Copy File: " & root & fi.Name)
> End Sub
> Thank you
>
Author
15 Aug 2006 1:14 PM
peter.meth
Thanks.  That worked perfectly.

GhostInAK wrote:
Show quoteHide quote
> Hello peter.m***@rci.rogers.com,
>
> The label gets updated when the message to repaint it is processed.  So massage
> the message pump.  Application.DoEvents.
>
> -Boo
>
> > Hi All,
> >
> > I am making a file manager type of application and am trying to
> > duplicate Windows Explorer's behaviour when copying files; ie, display
> > a second form with the copy files animation.  As it is copying files,
> > it should show the file name and folder.  For testing purposes I am
> > sleeping for 1 second to simulate a file being copied.  My problem is
> > that when I run the code, the labels do not change to reflect the file
> > names until the very end, when only the last filename will be
> > reflected.  However, if I put a message box before the sleep, the
> > labels get set as I expected.  I have tried using a timer and a long
> > loop instead of the sleep, and I have also tried a textbox instead of
> > a label, but all produce the same behaviour.  Here is the offending
> > code - it is called by a loop which loops through all the files to be
> > copied.
> >
> > Public Sub ProcessFile(ByVal root As String, ByVal pathToFile As
> > String)
> > Dim fi As New FileInfo(pathToFile)
> > lblFileBeingProcessed.Text = fi.Name
> > lblFromDirectoryToDirectory.Text = "From '" & root & "' to '" &
> > root & "'"
> > 'MsgBox("Copy File: " & root & fi.Name)
> > System.Threading.Thread.Sleep(1000) ' Sleep for 1 second
> > bytesProcessed += fi.Length
> > ProgressBar1.Value = Math.Min(100, Math.Round(bytesProcessed /
> > bytesToProcess * 100))
> > myParentForm.ListBox1.Items.Add("Copy File: " & root & fi.Name)
> > End Sub
> > Thank you
> >
Author
16 Aug 2006 4:26 PM
Chris Dunaway
> GhostInAK wrote:
> > Hello peter.m***@rci.rogers.com,
> >
> > The label gets updated when the message to repaint it is processed.  So massage
> > the message pump.  Application.DoEvents.
> >

Be careful with DoEvents in a loop, it can allocate more memory than a
simple Sleep(0) call.  You could also call the Refresh method on the
label which should work as well.  See the following links about
DoEvents:

http://blogs.msdn.com/tmiller/archive/2003/11/07/57524.aspx
http://www.codinghorror.com/blog/archives/000159.html