0

I've created a copy file method that i would like to wrap around a background worker. I would like the copy file method to report a string back to the UI thread (change a label) on the current file name its on. I can't seem to get it to work and i'm getting cross thread ui errors. Here is my code.

Do Work

      //Textbox Array Values
        // 0 = computername
        // 1 = username
        // 2 = password
        string[] tbvalues = (string[])e.Argument;


        string computer = tbvalues[0];
        string user = tbvalues[1];
        string pass = tbvalues[2];

        string userfavorites = @"\\" + computer + @"\C$\Users\" + user + @"\Favorites";

        string hdrivepath = @"\\dist-win-file-3\homes\" + user + @"\Favorites";

        string SourcePath = userfavorites;
        string DestinationPath = hdrivepath;

This part is a custom class used to impersonate a user

        using ( new Impersonator( user, "Domain.org", pass ) )
        {
            DirectoryInfo sp = new DirectoryInfo(SourcePath);
            DirectoryInfo dp = new DirectoryInfo(DestinationPath);

            CopyAll(sp, dp, bgwBackup, e);



        }
}

Method

   public void CopyAll(DirectoryInfo source, DirectoryInfo target, BackgroundWorker worker, DoWorkEventArgs e)
    {


        // Check if the target directory exists, if not, create it.
        if (Directory.Exists(target.FullName) == false)
        {
            Directory.CreateDirectory(target.FullName);
        }

        // Copy each file into it’s new directory.
        foreach (FileInfo fi in source.GetFiles())
        {

            //THIS IS THE STRING I WOULD LIKE TO RELAY TO THE BACKGROUND WORKER REPORT PROGRESS
            string currentfile = "Copying " + target.FullName.ToString() + fi.Name.ToString();                

            fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);

            worker.ReportProgress(0, currentfile);


        }

        // Copy each subdirectory using recursion.
        foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
        {
            DirectoryInfo nextTargetSubDir =
                target.CreateSubdirectory(diSourceSubDir.Name);
            CopyAll(diSourceSubDir, nextTargetSubDir, bgwBackup, e);
        }
    }

    private void cboBackuppwdshow_CheckedChanged(object sender, EventArgs e)
    {
        if (cboBackuppwdshow.Checked == true)
        {
            txtBackuppwd.UseSystemPasswordChar = false;
        }

        else
        {
            txtBackuppwd.UseSystemPasswordChar = true;
        }
    }

Report Progress

 private void bgwBackup_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        lblBackupStatus.Text = e.UserState.ToString();
    }

Button Event

 private void btnBackupqueue_Click(object sender, EventArgs e)
    {
        // Kickoff the worker thread to begin it's DoWork function.

        string[] tbvalues = {ddlBackupselectcomp.Text, ddlBackupselectuser.Text, txtBackuppwd.Text};


        backupWorker.RunWorkerAsync(tbvalues);
        lblBackupStatus.Text = "Backup process started please wait... ";
    }

Any suggestions ? Thanks!

Boundinashes6
  • 297
  • 3
  • 8
  • 23
  • It has been answered here : http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c – Simon V. May 06 '13 at 22:21
  • There are plenty of questions on marshalling UI calls across thread. Here - http://stackoverflow.com/questions/1334799/using-a-background-worker-update-a-progressbar-on-the-progress-of-a-recursive?rq=1 - for example. – ChrisF May 06 '13 at 22:21
  • Can you show us how you create the backgroundworker and where you start it? – Kenneth May 06 '13 at 22:24

1 Answers1

1

Problem comes from this line of code:

lblBackupStatus.Text = e.UserState.ToString();

This is executed from another thread. You need to use the Invoke method to update your label:

this.InvokeEx(c => this.lblBackupStatus.Text = e.UserState.ToString());

Here is the helper methods I usually use for control invoke:

public static class ControlExtensions
{
    public static TResult InvokeEx<TControl, TResult>(this TControl control,
                                                Func<TControl, TResult> func) where TControl : Control
    {
        return control.InvokeRequired
                ? (TResult)control.Invoke(func, control)
                : func(control);
    }

    public static void InvokeEx<TControl>(this TControl control,
                                            Action<TControl> func) where TControl : Control
    {
        control.InvokeEx(c => { func(c); return c; });
    }

    public static void InvokeEx<TControl>(this TControl control, Action action)
        where TControl : Control
    {
        control.InvokeEx(c => action());
    }
}

PS: I've typed directly, it may not compile (except the helper methods)

Fabske
  • 2,106
  • 18
  • 33
  • The "lblBackupStatus.Text = e.UserState.ToString();" line is called from the report progress event, and therefor from the thread the worker was created on. it looks ok... – Avi Turner May 07 '13 at 03:55
  • Thanks for you advice, where would i put that class at in my program ? EDIT: figured it out, this solved my issue and works great ! thank you so much :) – Boundinashes6 May 07 '13 at 14:10