Reputation: 351
I setup the following test based on another project im working on and cant seem to get the progress bar to show the status as its copying the files
BackgroundWorker workerThread = null;
public Form2()
{
InitializeComponent();
InstantiateWorkerThread();
}
private void InstantiateWorkerThread()
{
workerThread = new BackgroundWorker();
workerThread.ProgressChanged += WorkerThread_ProgressChanged;
workerThread.DoWork += WorkerThread_DoWork;
workerThread.WorkerReportsProgress = true;
workerThread.WorkerSupportsCancellation = true;
}
private void WorkerThread_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
lblStopWatch.Text = ("Progress: " + e.ProgressPercentage.ToString() + "%");
progressBar1.Value = e.ProgressPercentage;
}
private void WorkerThread_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i <= 100; i++)
{
// Report progress to 'UI' thread
workerThread.ReportProgress(i);
// Simulate long task
copytest();
}
}
private void btnStart_Click(object sender, EventArgs e)
{
workerThread.RunWorkerAsync();
}
private void copytest()
{
string pathFrom = @"C:\Test\WA8\CLR";
string pathTo = @"C:\Test\Test";
foreach (String file in Directory.GetFiles(pathFrom))
{
// Copy the current file to the new path.
File.Copy(file, Path.Combine(pathTo, Path.GetFileName(file)), true);
}
}
I am using this how to for my example i found online
I also tried this example with my code and not working Second how to attempted
What am i doing wrong with this setup? The copy works and takes about 30 seconds because there is only 50 files..
My form is simple, Button, Progressbar and Label
Also i guess to correct my earlier statement below, the Label text does show up, the percentage that should be shown does not.. So the label is being displayed as seen in the screen shot
So something interesting, i took a screen shot of the form earlier which meant i had to run the application to show the label in the shot, well since i didnt close the application while i was posting, i came back to it after my earlier post and found that things updated, but not correctly. All 59 files were copied over, but even though the copy had already completed, the the progress bar only showed partially green and the label reflected 5%. Why would the process run, complete and the progress bar only reflect 5% complete?
Upvotes: 0
Views: 95
Reputation: 351
My current version of Theodors Example which does work now..
string[] filestoCopy = Directory.GetFiles(pathFrom);
for (int i = 0; i <= filestoCopy.Length; i++)
{
int u = (i * 100 / filestoCopy.Length);
// Report progress to 'UI' thread
workerThread.ReportProgress(u);
// Simulate long task
File.Copy(filestoCopy[i], Path.Combine(pathTo, Path.GetFileName(filestoCopy[i])), true);
}
workerThread.ReportProgress(100);
So one last question about this, i have a need to place a status and progress bar in the main production app, but it doesnt copy files, it creates datatables, how easy is it to implement all the above in those cases? Ill post another question, but wanted to see if anyone can shed some light on it here since this "example" works now.
Upvotes: 0
Reputation: 43400
To report the progress you must know how many files you have to copy in total, and how many files have been copied so far. To do this you must begin by storing the paths returned from Directory.GetFiles
to a variable:
private void WorkerThread_DoWork(object sender, DoWorkEventArgs e)
{
const string pathFrom = @"C:\Test\WA8\CLR";
const string pathTo = @"C:\Test\Test";
string[] filePaths = Directory.GetFiles(pathFrom);
for (int i = 0; i < filePaths.Length; i++)
{
int currentProgress = (i * 100) / filePaths.Length;
workerThread.ReportProgress(currentProgress);
var filePath = filePaths[i];
var fileName = Path.GetFileName(filePath);
var newFilePath = Path.Combine(pathTo, fileName);
File.Copy(filePath, newFilePath, overwrite: true);
}
workerThread.ReportProgress(100);
}
Upvotes: 1