【问题标题】:Progress bar does not update on separate form from VSTO addin进度条不会在来自 VSTO 插件的单独表单上更新
【发布时间】: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


【解决方案1】:

虽然这是使用委托的旧样式,但您可能需要检查表单是否可用于更新。下面是较旧的代码 - 有些示例使用不需要委托的较新语法 - 但通常说明了解析。

    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;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-12
    • 1970-01-01
    • 2013-10-04
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    相关资源
    最近更新 更多