2013-05-06 37 views
0

我已经创建了一个复制文件方法,我想环绕后台工作人员。我想复制文件方法报告一个字符串返回到当前文件名称上的UI线程(更改标签)。我似乎无法得到它的工作,我得到跨线程UI错误。这是我的代码。来自方法的背景工作人员报告字符串

做工作

 //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; 

这部分是用来模拟用户

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

      CopyAll(sp, dp, bgwBackup, e); 



     } 
} 

方法

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; 
     } 
    } 

报告进展

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

按钮事件

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... "; 
    } 

有什么建议?谢谢!

+0

它已经在这里得到解答:http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c – 2013-05-06 22:21:43

+0

有很多有关跨线程对UI调用进行编组的问题。这里 - 例如,http://stackoverflow.com/questions/1334799/using-a-background-worker-update-a-progressbar-on-the-progress-of-a-recursive?rq=1。 – ChrisF 2013-05-06 22:21:57

+0

你能告诉我们你是如何创建背景工作的吗? – Kenneth 2013-05-06 22:24:44

回答

1

问题来自这行代码:

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

这是从另一个线程执行。您需要使用Invoke方法来更新您的标签:

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

这里是辅助方法,我通常使用的控制调用:

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:我直接输入,它可能无法编译(除了帮手方法)

+0

“lblBackupStatus.Text = e.UserState.ToString();”行从报告进度事件中调用,因此从创建工作者的线程中调用。它看起来不错... – 2013-05-07 03:55:30

+0

感谢您的意见,我会在哪里把我的课程放在我的程序中? 编辑:弄明白了,这解决了我的问题,并且效果很棒!非常感谢 :) – Boundinashes6 2013-05-07 14:10:42