HiTech
HiTech

Reputation: 1003

Update Progress bar using backgroundworker during file transfer

My goal here is:

In my code, you can see i added a method to get the size of the directory. This may, or may not be important but i left it in there anyways. Please ignore if irrelevant to my 'goal'. I also have a slight feeling the way I'm going about copying the data might be the issue but i really have no idea. I'm still new to all this. Thank you in advance and i appreciate any assistance!

EDIT I found this amazing example here. This answers most of my question but I'm still stuck on getting this to correctly run on a background thread. Should i be using backgroundworker to do this?

private void button1_Click(object sender, EventArgs e)
{   
    //Start background worker thread. Passes computer name the user entered       
    backgroundWorker1.RunWorkerAsync(comboBox1.Text);            
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    //Computer name user entered
    string PCName = (string)e.Argument;

    string DestinationPath = @"Remote PC C: Drive";
    string SourcePath = @"Network share";

    //Get File Size            
    DirectoryInfo dInfo = new DirectoryInfo(SourcePath);
    long sizeOfDir = DirectorySize(dInfo, true);

    //Use to output. File Size in MB
    double size = sizeOfDir / (1024 * 1024);                        

    //Creates Folder on remote PC
    Directory.CreateDirectory(DestinationPath);            

    //Create all of the directories
    foreach (string dirPath in Directory.GetDirectories(SourcePath, "*", SearchOption.AllDirectories))
    {
        Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));
    }

    //Copy all the files
    foreach (string newPath in Directory.GetFiles(SourcePath, "*.*", SearchOption.AllDirectories))
    {
        File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath));                
    }                       
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    MessageBox.Show("Done");
}

static long DirectorySize(DirectoryInfo dInfo, bool includeSubDir)
{
    // Enumerate all the files
    long totalSize = dInfo.EnumerateFiles()
                          .Sum(file => file.Length);

    // If Subdirectories are to be included
    if (includeSubDir)
    {
        // Enumerate all sub-directories
        totalSize += dInfo.EnumerateDirectories()
                          .Sum(dir => DirectorySize(dir, true));
    }
    return totalSize;
}   

Upvotes: 0

Views: 5926

Answers (1)

Tarec
Tarec

Reputation: 3255

I believe you only need to add something like

int PercentageDone = 100* SizeOfFilesAlreadyCopied/TotalSizeOfAllFiles;

backgroundWorker1.ReportProgress(PercentageDone);

to your foreach copying loop. You could use a second argument in ReportProgress method to copy some other attributes to display, though you'd need to use some simple custom container class. Oh and remember, that your backgroundWorker1_ProgressChanged method should be assigned to the backgroundworker itself

backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);

or just by selecting it from backgroundWorker's properties menu.

Simple examples here

http://msdn.microsoft.com/en-us/library/cc221403%28v=vs.95%29.aspx

Upvotes: 1

Related Questions