【问题标题】:refreshing progressbar while thread is runing in c# (wpf)在 C# (wpf) 中运行线程时刷新进度条
【发布时间】:2015-05-04 08:12:26
【问题描述】:

我有一个带有按钮的 wpf 应用程序。

按钮启动过程的单击事件大约需要 30-50 秒。 我想在这个线程工作时使用进度条。 除此之外,我还尝试了 dispatcher.invoke。没有成功。

这是我的代码:

MainWindow.xaml.cs:

 public MainWindow()
    {
      some code.....
      Engine.ProgressEvent += (percent) => Dispatcher.Invoke(new Action(() => progressBarIndicator.Value = percent));
      some code...

    }
    private void btnFirmwareUpdate_Click(object sender, RoutedEventArgs e)
    {
      some code...
      send data to hardware using functions in Engine class
      Engine e = new Engine();
      e.update();
    }

我的 Engine.cs :

Class Engine 
{
    public static event Action<double> ProgressEvent;

    public update()
    {

      ProgressEvent(10);
    }
}

进度条确实以正确的值更新进度条,但 gui 不刷新。 任何一种解决方案?

【问题讨论】:

标签: c# wpf delegates invoke


【解决方案1】:

我建议使用BackgroundWorker 来更新您的Main UI

这是一个如何使用BackgroundWorker 实现progress bar 的示例

Progress bar

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多