2015-05-04 73 views
0

我有wpf应用程序和按钮。线程运行在c#(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并不令人耳目一新。 解决方案任何一个?

+0

阅读本实施progress bar一个例子: http://stackoverflow.com/questions/5789926/wpf-c-sharp-更新进度从 - 另一个线程 –

回答

2

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

下面是如何使用BackgroundWorker

Progress bar