0
votes

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..

First how to attempted

My form is simple, Button, Progressbar and Label Simple Form Layout

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?

Partial update

2
Does the lblStopWatch.Text get updated? - LarsTech
I'm unable to reproduce the problem unless I throw an exception in copytest(), then of course the debugger tells me about it. Is a debugger attached? Have you changed any properties of progressBar1 in the designer? - rfmodulator
The code you posted works fine (assuming of course, as noted in the previous comment, that the copytest() method completes without an exception). If you have a problem, the code must be different. You'll need to include a good minimal reproducible example that reliably reproduces the problem. Note that that may require including all initialization normally done in the *.Designer.cs file, in case you've made an error configuring the program in the Designer. - Peter Duniho
I have posted everything in my code behind. The text does not update nor does the progress bar. No exceptions occur, there are 59 files in the source folder, some are 1gb in size and like i said tranfering take about 30 to 45 seconds.. afer i run the code, all 59 files are in the destination folder, so the copy works. Only thing not work is the text and bar.. :( - CubanGT
I verified and the only thing i didnt post in the original code was this line, which i have added to the code above BackgroundWorker workerThread = null; Other than that, the entire Form2.cs code is above, like i said, this was a simple test for concept before i try and on my production application.. - CubanGT

2 Answers

1
votes

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);
}
0
votes

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.