[英]Background worker report string from a method
我創建了一個復制文件方法,我想將其包裹在后台工作人員周圍。 我希望復制文件方法將字符串報告給當前文件名所在的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... ";
}
有什么建議么 ? 謝謝!
問題來自以下代碼行:
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:我直接輸入了,可能無法編譯(助手方法除外)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.