【发布时间】:2019-06-29 01:18:10
【问题描述】:
在 Word VSTO 插件中单击按钮时,我想显示带有进度条的表单并更新其值。
即使我使用了 BackgroundWorker 及其事件(DoWork、ProgressChanged),进度条的进度也不会相应更新
private void extractDataButton_Click(object sender, RibbonControlEventArgs e)
{
//On button click of addin
ProgressNotifier progressNotifier = new ProgressNotifier();
progressNotifier.Show();
progressNotifier.UpdateProgressBar(10);
// Does the work which lasts few seconds
HandleRetrievedData(data);
progressNotifier.UpdateProgressBar(100);
progressNotifier.Close();
}
// Progress bar form
public partial class ProgressNotifier : Form
{
public ProgressNotifier()
{
InitializeComponent();
}
public void UpdateProgressBar(int progress)
{
backgroundWorker1.ReportProgress(progress);
progressBar_extractionProgress.Update();
}
private void backgroundWorker1_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
this.progressBar_extractionProgress.Value = e.ProgressPercentage;
}
}
【问题讨论】:
-
我相信这是因为 BackgroundWorker 在您的 Main-/UI-Thread 的单独线程上运行,因此您必须调用 UI-Thread 上的更改:@987654322 @
-
这是一个 WinForms 应用程序,因此 Application.Current 返回 null。有没有其他方法可以调用 Dispatcher?
-
我认为你可以直接调用控件本身,因为我使用 WinForms 已经有一段时间了,但试试看:
this.progressBar_extractionProgress.Invoke(() => this.progressBar_extractionProgress.Value = e.ProgressPercentage);
标签: c# winforms progress-bar vsto