|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Label changes not being reflected until loop is doneI 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 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 > 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 > > > GhostInAK wrote: Be careful with DoEvents in a loop, it can allocate more memory than a> > 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. > > 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
Active Directory
Populating date into SQL Option strict question. Compiling list of items that WinForms can do, WebForms cannot Waiting for com program to terminate Creating Reports from SQL data Viewing machine data source names what have they done to the left function Filename from Open With how to format text for email message |
|||||||||||||||||||||||