[英]Progress bar does not update on separate form from VSTO addin
On button click in Word VSTO addin, I want to show the form with progress bar and update its value. 在按钮上单击Word VSTO加载项,我想显示带有进度栏的表单并更新其值。
Even though I used BackgroundWorker and its events (DoWork, ProgressChanged), progress of the progress bar does not update accordingly 即使使用了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;
}
}
Although this is an older style using delegates, you might need a check that the form is available for updating. 尽管这是使用委托的较旧样式,但您可能需要检查表单是否可用于更新。 Below is older code - there are examples using newer syntax not requiring delegates - but generally illustrates a resolve.
以下是较旧的代码-有些示例使用较新的语法而不要求委托-但通常说明了解决方法。
private delegate void StatusMessage();
/// <summary>
/// Simple methods for setting active cube list before connecting
/// </summary>
private void SetDefaultNode()
{
if (this.ActiveCubeStatus.InvokeRequired)
{
StatusMessage d = new StatusMessage(SetDefaultNodeDirect);
this.Invoke(d);
}
else
{
SetDefaultNodeDirect();
}
}
/// <summary>
/// Simple methods for setting active cube list before connecting
/// </summary>
private void SetDefaultNodeDirect()
{
//clears treeveiw
ClearActiveCubes();
//create default inactive node
TreeNode nodeDefault = new TreeNode();
nodeDefault.Name = "Waiting";
nodeDefault.Text = "Waiting on connection...";
this.ActiveCubeStatus.Nodes.Add(nodeDefault);
nodeDefault = null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.