Updating Label during foreach loop

Hey guys, got a bit of a problem. I’ve made a launcher application. It works pretty well but there is a bit of quality of life I want to add into it, and it turns out it’s a lot more difficult than I thought it was…

When it’s launching big files, it can seem like it isn’t doing anything, so I thought “Hey, I’ll put the files it’s currently working on in there to signify it is working”.

I was already writing the files to the console during this process, so I assumed it would be easy.

Here is my code:

 public static void Copy(string connectionString, string DestinationPath, Label ProgresserLabel) { DirectoryInfo diSource = new DirectoryInfo(connectionString); DirectoryInfo diTarget = new DirectoryInfo(DestinationPath); CopyAll(diSource, diTarget, ProgresserLabel); } public static void CopyAll(DirectoryInfo source, DirectoryInfo target, Label ProgresserLabel) { Directory.CreateDirectory(target.FullName); // Copy each file into the new directory. foreach (FileInfo fi in source.GetFiles()) { Console.WriteLine(@"Copying {0}{1}", target.FullName, fi.Name); ProgresserLabel.Visible = true; ProgresserLabel.Text = fi.Name; fi.CopyTo(Path.Combine(target.FullName, fi.Name), true); } // Copy each subdirectory using recursion. foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) { DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name); CopyAll(diSourceSubDir, nextTargetSubDir, ProgresserLabel); } } 

So this is the bit that actually copies down files when an update has been released. So In there you’ll see a foreach loop with the ProgresserLabel in there.

I already get the file being processed and write it to the console. Which works great. However, the label text changes are not working so well. It only updates the label after it finishes the CopyAll function. So nothing appears, and then the last object to be copied appears at the very end. But by then the launcher is done…

Any way to update the label per item it iterates through? The console does it fine, so I’m not sure why the label doesn’t?


I should also mention that the ProgresserLabel I’m passing through is this. Might be useful!

 private static Label ProgresserLabel; public Launcher() { InitializeComponent(); ProgresserLabel = Label; } 

submitted by /u/Alundra828
[link] [comments]

Leave a Reply