Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 717987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:28:30+00:00 2026-05-14T05:28:30+00:00

I have wrote an application that syncs two folders together. The problem with the

  • 0

I have wrote an application that syncs two folders together. The problem with the program is that it stops responding whilst copying files. A quick search of stack-overflow told me I need to use something called a background worker. I have read a few pages on the net about this but find it really hard to understand as I’m pretty new to programming. How can I simply put all of the File.Copy(….) commands into their own background worker (if that’s even how it works)? Below is the code for the button click event that runs the sub procedure and the sub procedure I wish to use a background worker on all the File.Copy lines.

Any help will be greatly appreciated as after this the program will pretty much be finished 😀

EDIT

Using the advice of Veer below I have changed my code accordingly however I keep getting the follwoing errors:

Error CS1061: Type Gtk.ProgressBar' does not contain a definition forInvokeRequired’ and no extension method InvokeRequired' of typeGtk.ProgressBar’ could be found (are you missing a using directive or an assembly reference?) (CS1061) (Sync-GUI v2)

Error CS1061: Type Gtk.ProgressBar' does not contain a definition forBeginInvoke’ and no extension method BeginInvoke' of typeGtk.ProgressBar’ could be found (are you missing a using directive or an assembly reference?) (CS1061) (Sync-GUI v2)

Below is my code.

Button Click Event:

protected virtual void OnBtnSyncClicked (object sender, System.EventArgs e)
{
    //sets progress bar to 0
    prgProgressBar.Fraction = 0;

    //resets values used by progressbar
    dblCurrentStatus = 0;
    dblFolderSize = 0;

    //tests if user has entered the same folder for both target and destination
    if (fchFolder1.CurrentFolder == fchFolder2.CurrentFolder)
    {
        //creates message box
        MessageDialog msdSame = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Close, "You cannot sync two folders that are the same");
        //sets message box title
        msdSame.Title="Error";
        //sets respone type
        ResponseType response = (ResponseType) msdSame.Run();
        //if user clicks on close button or closes window then close message box
        if (response == ResponseType.Close || response == ResponseType.DeleteEvent) {
            msdSame.Destroy();
        }
        return;
    }

    //tests if user has entered a target folder that is an extension of the destination folder
    // or if user has entered a desatination folder that is an extension of the target folder
    if (fchFolder1.CurrentFolder.StartsWith(fchFolder2.CurrentFolder) || fchFolder2.CurrentFolder.StartsWith(fchFolder1.CurrentFolder))
    {
        //creates message box
        MessageDialog msdContains = new MessageDialog(this, DialogFlags.Modal, MessageType.Error, ButtonsType.Close, "You cannot sync a folder with one of its parent folders");
        //sets message box title
        msdContains.Title="Error";          
        //sets respone type and runs message box
        ResponseType response = (ResponseType) msdContains.Run();
        //if user clicks on close button or closes window then close message box
        if (response == ResponseType.Close || response == ResponseType.DeleteEvent)
        {
            msdContains.Destroy();
        }
        return;
    }   

    //creates background worker
    BackgroundWorker bwBackgroundWorker = new BackgroundWorker();
    bwBackgroundWorker.DoWork += new DoWorkEventHandler(bwBackgroundWorkerDoWorkFolder1);
    //starts background worker
    bwBackgroundWorker.RunWorkerAsync();

    //informs user process is complete
    prgProgressBar.Text = "Finished";
}

Background Worker Do Work Event:

private void bwBackgroundWorkerDoWorkFolder1 (object sender, DoWorkEventArgs e)
{
    //Gets total file size of folder 1
    TotalFileSizeFolder1(fchFolder1.CurrentFolder);
    //Syncs folder 1
    SyncFolder1(fchFolder1.CurrentFolder, fchFolder2.CurrentFolder);
}

TotalFileSizeFolder1 Sub Routine:

protected void TotalFileSizeFolder1 (string strCurrentDirectory)
{
    //inform user that file sizes are being gathered
    if (prgProgressBar.InvokeRequired)
    {
        prgProgressBar.BeginInvoke(new MethodInvoker(delegate {prgProgressBar.Text="Getting total size of " + strCurrentDirectory;}));
    }

    //string array of all the directories in directory
    string[] staAllDirectories = Directory.GetDirectories(strCurrentDirectory);
    //string array of all the files in directory
    string[] staAllFiles = Directory.GetFiles(strCurrentDirectory);

    foreach (string strFile in staAllFiles)
    {
        //saves new file info called FileSize
        FileInfo FileSize = new FileInfo(strFile);
        //adds file size 
        dblFolderSize = dblFolderSize + FileSize.Length;
        //pulses progress bar to indicate some movement
        if (prgProgressBar.InvokeRequired)
    {
        prgProgressBar.BeginInvoke(new MethodInvoker(delegate {prgProgressBar.Pulse();}));
    }

    }


    foreach (string strDirectory in staAllDirectories)
    {
        TotalFileSize(strDirectory);
    }
    //delete text from progress bar

    if (prgProgressBar.InvokeRequired)
    {
        prgProgressBar.BeginInvoke(new MethodInvoker(delegate {prgProgressBar.Text="";}));
    }

}   

SyncFolder1 Sub Routine:

protected void SyncFolder1 (string strFolder1, string strFolder2)
{
    //string array of all the directories in directory
    string[] staAllDirectories = Directory.GetDirectories(strFolder1);
    //string array of all the files in directory
    string[] staAllFiles = Directory.GetFiles(strFolder1);

    //loop over each file in directory
    foreach (string strFile in staAllFiles)
    {
        //string of just the file's name and not its path
        string strFileName = System.IO.Path.GetFileName(strFile);
        //string containing directory in target folder
        string strDirectoryInsideFolder1 = System.IO.Path.GetDirectoryName(strFile).Substring(strFolder1.Length);

        //inform user as to what file is being copied
        if (prgProgressBar.InvokeRequired)
        {
            prgProgressBar.BeginInvoke(new MethodInvoker(delegate {prgProgressBar.Text="Syncing " + strFile;}));
        }

        //tests if file does not exist in destination folder
        if (!File.Exists(fchFolder2.CurrentFolder + "/" + strDirectoryInsideFolder1 + "/" + strFileName))
        {
            //if file does not exist copy it to destination folder, the true below means overwrite if file already exists
            File.Copy (strFile, strFolder2 + "/" + strDirectoryInsideFolder1 + "/" + strFileName, true);
        }

        //tests if file does exist in destination folder
        if (File.Exists(strFolder2 + "/" + strDirectoryInsideFolder1 + "/" + strFileName))
        {
            //long (number) that contains date of last write time of target file
            long lngFolder1FileDate = File.GetLastWriteTime(strFile).ToFileTime();
            //long (number) that contains date of last write time of destination file
            long lngFolder2FileDate = File.GetLastWriteTime(strFolder2 + "/" + strDirectoryInsideFolder1 + "/" + strFileName).ToFileTime();

            //tests if target file is newer than destination file
            if (lngFolder1FileDate > lngFolder2FileDate)
            {
                //if it is newer then copy file from target folder to destination folder
                File.Copy (strFile, strFolder2 + "/" + strDirectoryInsideFolder1 + "/" + strFileName, true);
            }   
        }
        //gets current file size
        FileInfo FileSize = new FileInfo(strFile);
        //sets file's filesize to dblCurrentStatus and adds it to current total of files 
        dblCurrentStatus = dblCurrentStatus + FileSize.Length;
        double dblPercentage = dblCurrentStatus/dblFolderSize;
        if (prgProgressBar.InvokeRequired)
        {
            prgProgressBar.BeginInvoke(new MethodInvoker(delegate {prgProgressBar.Fraction = dblPercentage;}));
        }
    }

    //loop over each folder in target folder
    foreach (string strDirectory in staAllDirectories)
    {
        //string containing directories inside target folder but not any higher directories
        string strDirectoryInsideFolder1 = strDirectory.Substring(strFolder1.Length);
        //tests if directory does not exist inside destination folder
        if (!Directory.Exists(strFolder2 + "/" + strDirectoryInsideFolder1))
        {
            //it directory does not exisit create it
            Directory.CreateDirectory(strFolder2 + "/" + strDirectoryInsideFolder1);
        }
        //run sync on all files in directory
        SyncFolders(strDirectory, strFolder2);
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:28:30+00:00Added an answer on May 14, 2026 at 5:28 am

    Initialise your background worker object

    BackgroundWorker bw = new BackgroundWorker();
    bw.DoWork += new DoWorkEventHandler(bw_DoWork);
    

    Use this code

    bw.RunWorkerAsync();  // Calls the bw_DoWork method
    

    in the place of

    //runs SyncTarget procedure      
    SyncTarget(fchTarget.CurrentFolder);
    //gets folder size of destination folder              
    FileSizeOfDestination(fchDestination.CurrentFolder);              
    

    Define the DoWork method

    private void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        SyncTarget(fchTarget.CurrentFolder);
        FileSizeOfDestination(fchDestination.CurrentFolder);
    }
    

    I don’t think using two background workers here is necessary because both the methods are involved in IO operations.

    You can also well make use of RunWorkerCompleted and ProgressChanged

    http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

    And I also see that you access UI elements in your SyncTarget method. You cannot access your UI element from another thread. You have to use the BeginInvoke method to accomplish this

    if (prgProgressBar.InvokeRequired)
    {
        prgProgressBar.BeginInvoke(new MethodInvoker(delegate { prgProgressBar.Text="Syncing " + strFile; }));
    }
    

    You can also achieve this using dispatcher.

    Dispatcher UIDispatcher = Dispatcher.CurrentDispatcher;  // Use this code in the UI thread
    
    UIDispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>  
    {  
        // access your prgProgressBar here 
    })); 
    

    There are many question in SO on dispatcher and Cross threading operations. You can browse them out.

    I used prgProgressBar just for an example. But i would recommend you to use the progress bar in the ProgressChanged method.

    bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
    
    private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        prgProgressBar.Value = e.ProgressPercentage;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.