Reputation: 321
I'm using WPF MVVM and have a progress bar to report progress on a task, using background worker. It works except I'm having a problem if I make the progress bar collapsed when the work completes, if I do that the progress bar never gets to the end. If I leave the progress bar visible it's fine, but I don't want the progress bar to be visible when the task is finished. Can someone tell me where I'm going wrong? In the below GenerateOutput is just a function that creates some files and updates progress as it goes. ShowSummary() is something that needs to run when the task is finished. I tried putting a Thread.Sleep in the RunWorkerCompleted just so the user can actually see the completed bar before its collapsed but you still don't see it complete.
public void BackgroundWorkerGenerateOutputProgress()
{
ProgressVisibility = Visibility.Visible;
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(DoWork);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(RunWorkerCompleted);
worker.RunWorkerAsync();
}
void DoWork(object sender, DoWorkEventArgs e)
{
if (IsInProgress)
return;
CurrentProgress = 0;
IsInProgress = true;
BackgroundWorker worker = sender as BackgroundWorker;
GenerateOutput();
}
void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
IsInProgress = false;
ProgressVisibility = Visibility.Collapsed;
ShowSummary();
}
EDIT: I have changed it to the below, I'm still not seeing the progress bar get to the end:
public void BackgroundWorkerGenerateOutputProgress()
{
ProgressVisibility = Visibility.Visible;
CurrentProgress = 0;
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += new DoWorkEventHandler(DoWork);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(RunWorkerCompleted);
worker.ProgressChanged += new ProgressChangedEventHandler(ProgressChanged);
worker.RunWorkerAsync();
}
private void ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
CurrentProgress = e.ProgressPercentage;
}
void DoWork(object sender, DoWorkEventArgs e)
{
if (IsInProgress)
return;
IsInProgress = true;
BackgroundWorker worker = sender as BackgroundWorker;
DoIt(worker);
}
void DoIt(BackgroundWorker worker)
{
worker.ReportProgress(20);
Thread.Sleep(1000);
worker.ReportProgress(40);
Thread.Sleep(1000);
worker.ReportProgress(60);
Thread.Sleep(1000);
worker.ReportProgress(80);
Thread.Sleep(1000);
worker.ReportProgress(100);
}
void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
IsInProgress = false;
ShowSummary();
System.Threading.Thread.Sleep(2000);
ProgressVisibility = Visibility.Collapsed;
}
the xaml (sorry can't format):
<ProgressBar HorizontalAlignment="Left" Value="{Binding CurrentProgress, Mode=OneWay}" Height="20" Width="200" Visibility="{Binding Path=ProgressVisibility}" Minimum="0" Maximum="100" />
Upvotes: 1
Views: 3134
Reputation: 45096
Try ReportProgress as shown in this link. You don't have WorkerReportsProgress = true
If you are setting it at the end of GenerateOutput() then it is set to Visibility.Collapsed; before the UI is rendered.
Upvotes: 1
Reputation: 5605
I haven't tryied this but logically thinking I would do this apporch. Set the value of progress to its maxvalue before you hide it.
void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
CurrentProgress = 0; //Set this to max value
IsInProgress = false;
ProgressVisibility = Visibility.Collapsed;
ShowSummary();
}
Upvotes: 0