1

I have already spent one whole week researching on this but things are not working for me.

I have a WPF UI with a progress bar. My business logic is in a seperate DLL. I want to update the progress bar from the DLL. I have a SetProgress(int) method on the UI side. I am passing this method as a Action delegate to the DLL and calling it from within the DLL to update progress. But I notice that the progress bar is not updating at the real time. Instead, after all the processing is done the progress bar suddenly reaches 100% which is not the desirable behaviour.

I also tried BackGroundWorker but I was unable to get a real time progress bar update with that either. Here is my approach on the BackGroundWorker way:

UI Code:

public partial class MainWindow : Window
{

    public BackgroundWorker bw;
    public MainWindow()
    {            
        bw = new BackgroundWorker();
        bw.WorkerReportsProgress = true;
        ProgressChangedEventHandler(OnProgressChanged);                 
    }

    public void OnProgressChanged(object sender, ProgressChangedEventArgs args)
    {            
        SetProgress(args.ProgressPercentage);
    }

    public void SetProgress(int progress)
    {      
        progressBar.Text = percent.ToString();
    }

    public void btn_click()
    {
         DoSomething(bw);
    }
}

DLL Code:

     Public void DoSomething(BackgroundWorker bw)
     {
           bw.DoWork += new DoWorkEventHandler(bw_DoWork);
           bw.RunWorkerAsync();
     }


     public void bw_DoWork(object sender, DoWorkEventArgs e)
     {
          BackgroundWorker worker = sender as BackgroundWorker;
          for (int i = 0; i < 100; i++)
          { 
                worker.ReportProgress(i + 1);
          }
     }

I do not know where I am going wrong because this code too doesnt update the progress bar at real time.

Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
  • This may be a solution : http://geekswithblogs.net/NewThingsILearned/archive/2008/08/25/refresh--update-wpf-controls.aspx – PaulF Jun 23 '15 at 09:07
  • I also found the following comment somewhere _If you're using Windows 7 (don't know about Vista), I have to change the DispatcherPriority to Input (or lower) before it'll work._ – PaulF Jun 23 '15 at 09:09

1 Answers1

0

Where are you 'hooking up' the event ProgressChanged to your OnProgressChanged method on the BackgroundWorker? Extend your MainWindow constructor:

public MainWindow()
{            
    bw = new BackgroundWorker();
    bw.WorkerReportsProgress = true;
    // this....
    bw.ProgressChanged += OnProgressChanged; 
}

Don't forget, if you want to update a UI element from another thread in WPF, you must invoke it on the Dispatcher (thread).

example:

public void OnProgressChanged(object sender, ProgressChangedEventArgs args)
{            
    this.Dispatcher.Invoke( () => SetProgress(args.ProgressPercentage) );
}

public void SetProgress(int progress)
{      
    progressBar.Text = percent.ToString();
}

This way the UI element is updated on the right thread.

Here's some more information about updating UI controls on other threads: Change WPF controls from a non-main thread using Dispatcher.Invoke

Community
  • 1
  • 1
Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
  • Sorry, I forgot to mention, I did hook up the ProgressChanged Method in my constructor, like this bw.ProgressChanged += new ProgressChangedEventHandler(OnProgressChanged); – user2990857 Jun 23 '15 at 15:57
  • I added the Dispatcher.Invoke in the OnProgressChanged() method, yet the behaviour is the same, the progress bar updates only after all the calculations are over and suddenly dashes to 100%. – user2990857 Jun 23 '15 at 15:59
  • within your `bw_DoWork`, there isn't much to do, or anything time consuming... does it go too fast? – Jeroen van Langen Jun 23 '15 at 17:23
  • I have tried adding a lot of work but the result is the same. I do not know why such a trivial task is not getting achieved :-( – user2990857 Jun 26 '15 at 09:15