Label changes not being reflected until loop is done

P

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
 
G

GhostInAK

Hello (e-mail address removed),

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

-Boo
 
P

peter.meth

Thanks. That worked perfectly.
Hello (e-mail address removed),

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

-Boo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top