【问题标题】:Updating Progress bar from a different thread in Windows 7从 Windows 7 中的不同线程更新进度条
【发布时间】:2014-10-20 08:41:34
【问题描述】:

我正在尝试更新一个包含标签和进度条的对话框。实际上,在我的应用程序中,我正在备份我的数据,在进行备份时,我想显示进度条和一个标签,显示当前正在备份的内容。我使用这个来调用进度

Application.Current.Dispatcher.Invoke(DispatcherPriority.ApplicationIdle, (Action)(() => { dialog.ProgressbarValue = "Backing up first item"; })); 

这在 Windows 8 上运行良好,但在 Windows 7 上没有任何更新。

【问题讨论】:

  • 您是否尝试过任何其他 DispatcherPriority,也许是 Background
  • 是的,DispatcherPriority.Send 即使在 Windows 8 上也不起作用。DispatcherPriority.Loaded 在 Windows 8 上工作,但在 Windows 7 上不起作用
  • 为什么不使用普通优先级?无论操作系统版本如何,这应该都能正常工作。
  • @AdrianFaciu 正常优先级现在完全更新任何内容。

标签: c# wpf progress-bar dispatcher


【解决方案1】:

您需要在文本框上应用刷新,以便在循环运行时显示所有文本。

您需要为 ExtensionMethods 提供一个静态类,其中包含您的刷新例程。

public static class ExtensionMethods
    {
        private static Action EmptyDelegate = delegate() { };

        public static void Refresh(this UIElement uiElement)
        {
            uiElement.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
        }
    }

每次设置 dialog.ProgressbarValue 后,从您的代码中调用此 extensionMethod 注意:你不需要在这个逻辑简单设置dialog.ProgressbarValue在代码中有Dispatcher,并使用dialog.Refresh()调用上面的刷新方法;

【讨论】:

    【解决方案2】:

    设置DispatcherPriority.ApplicationIdle实际上解决了这个问题

    【讨论】:

      猜你喜欢
      • 2023-03-28
      • 1970-01-01
      • 2015-07-30
      • 2012-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-29
      • 1970-01-01
      相关资源
      最近更新 更多