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.