Click here to Skip to main content
15,891,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I've been trying the last couple hours to understand and apply the "Progress Bar" and the "background worker".

I have to functions wich take..

The first one takes one argument wich is the combobox1.text.
The other one takes two : (combobox1.text,combobox2.text).


The first Function is :
public void CopyDirectory(string strSource, string strDestination)
        {
            string strDestinationFile = string.Empty;
            DirectoryInfo Dirinfo = new DirectoryInfo(strSource);
            if (!Directory.Exists(strDestination))
            {
                Directory.CreateDirectory(strDestination);
            }
            foreach (FileSystemInfo filesInfo in Dirinfo.GetFileSystemInfos())
            {
                strDestinationFile = Path.Combine(strDestination, filesInfo.Name);

                //Check If it's a file
                if (filesInfo is FileInfo)
                {
                    File.Copy(filesInfo.FullName, strDestinationFile, true);
                }
                else
                    // Recall The same Function
                    CopyDirectory(filesInfo.FullName, strDestinationFile);
					}
					}


The second one is:
public static long DirSize(DirectoryInfo d)
{
    long size = 0;
    FileInfo[] fis = d.GetFiles();
    foreach (FileInfo fi in fis)
    {
        size += fi.Length;
    }
    DirectoryInfo[] dis = d.GetDirectories();
    foreach (DirectoryInfo di in dis)
    {
        size += DirSize(di);
    }
    return size; //Returns the size of the whole directory for later usage on progress Bar



I didn't included the code for the progressbar value update in the code block as it's unimportant for now..

Since background worker can't access the control of the forms how am i supposed to implement these two functions i got in it ??

Thanks in advance, Stelios K.
Posted

See the following link.

http://www.dotneat.net/2009/02/10/BackgroundworkerExample.aspx[^]

[From WW: you were just missing the caret and right bracket for the new window link]
 
Share this answer
 
v3
First, neither of those functions has to access anything on the form. They're wholly self-contained.

CopyDirectory is a pretty basic routine for a BackgroundWorker. The only tricky part is getting two arguments through to the DoWork routine. There are a lot of ways to do it. One way is to create a simple struct that has the arguments you'll be passing and then pass that struct to the RunWorkerAsync method. Then, in the DoWork, you just pull out the arguments from the struct and call the copy function.

In the other routine, you just have to make sure that you set e.Result equal to the result of the DirSize function.

Then, in the RunWorkerCompleted event, you can report the result.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900