当我使用 WinForms 时,我会在我的 bg_DoWork
方法中这样做:
status.Invoke(new Action(() => { status.Content = e.ToString(); }));
status.Invoke(new Action(() => { status.Refresh(); }));
但是在我的 WPF 应用程序中,我收到一条错误消息,提示 Label
不存在 Invoke
。
如有任何帮助,我们将不胜感激。
使用 BackgroundWorker
中已经内置的功能。当您“报告进度”时,它会将您的数据发送到在 UI 线程上运行的 ProgressChanged
事件。无需调用 Invoke()
。
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
bgWorker.ReportProgress(0, "Some message to display.");
}
private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
status.Content = e.UserState.ToString();
}
确保设置 bgWorker.WorkerReportsProgress = true
以启用报告进度。
我是一名优秀的程序员,十分优秀!