【发布时间】:2018-08-31 00:03:20
【问题描述】:
我试图在后台工作进程期间更改表单上的标签但是它显示未处理的异常。我查看了错误,它说要像接受的答案在这里所说的那样调用它:Update label text in background worker winforms
我已经通过更改复选框列表中的值成功地做到了这一点,但我使用了相同的方法,并且对于它不会调用的标签,当我键入它时,我在代码中得到红色错误行。
我的后台工作者:
private void bw2_DoWork(object sender, DoWorkEventArgs args)
{
BackgroundWorker worker = sender as BackgroundWorker;
func.sshConnect();
for (int num = 0; num < checklist.Items.Count; num++)
{
if (checklist.GetItemChecked(num))
{
string project = checklist.Items[num].ToString();
lblStatus.Text = "Opening " + project + "..."; //error here
if (func.svnCheckoutProject(project))
{
lblStatus.Text = project + " Opened"; //same error here
func.sshRunCommand("echo " + project + " >> " + Properties.Settings.Default.serverUserFilesPath + Properties.Settings.Default.Username);
}
else
{
//error message
}
}
worker.ReportProgress(num * (100 / checklist.Items.Count));
}
}
我尝试用这个替换有错误的行,但在 Visual Studio 中它在调用下给它一个红线,并且不会让我构建它。
lblStatus.Invoke((MethodInvoker)delegate { lblStatus.Text = "Opening " + project + "..."; });
出现错误时,Visual Studio 将我指向此处:MSDN 我在复选框列表中使用了这种方法,它可以工作,但在标签上尝试它,它不起作用。
关于它为什么不起作用或其他方式的任何想法?
【问题讨论】:
标签: c# winforms backgroundworker