I have a Background worker in the ViewModel. When ever the Command for a button is executed I want to start the process in a new thread. Here is my sample Code
private void ShowValidations(object obj)
{
progress = "Please Wait....";
var worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += worker_DoWork;
worker.ProgressChanged += worker_ProgressChanged;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
}
And in the Do work method I have
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
var worker = sender as BackgroundWorker;
DoValidation();
for(int i=0;i<100;i++)
{
worker.ReportProgress(i); //Not sure here what should be written in the Background Worker Report progress event.
}
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
ViewSource.Source = _validationResults;
ViewSource.View.Refresh();
progress = "Validations Complete";
Isindeterminate = false;
//prvalue = 99;
}
DoValidation is method the fetches results for an other class just like a three tier architecture.
private void DoValidation()
{
try
{
if (!string.IsNullOrEmpty(_selectedEntity))
{
var jobEngine = new JobEngine();
var jobId = JobEntities[0].JobId;
jobEngine.ProcessValidation(_selectedEntity, jobId); //Get results from other class.
_validationResults = _validations.ValidationSummary(_selectedEntity, jobId); //_validationResults is an Observable Collection.
}
else
{
MessageBox.Show("Please select an Entity to validate");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "Sorry we are having problems with " + _selectedEntity);
}
}
How do I update the progress bar on the UI based on the work that is done. What should be in the ProgressChanged Event of the background worker.. Here is my View code.
<ProgressBar HorizontalAlignment="Left" Minimum="1" Maximum="99" Height="20" Margin="424,24,0,0" Grid.Row="1" Value="{Binding prvalue,Mode=OneWay}" VerticalAlignment="Top" Width="194"/>
<Label x:Name="LblprogressLabel" FontSize="14" Content="{Binding prvalue,Mode=OneWay}" HorizontalAlignment="Center" Margin="507,18,339,23" Grid.Row="1" Width="26"/>
<Label Content="{Binding progress,Mode=OneWay}" FontSize="16" FontWeight="Bold" HorizontalAlignment="Left" Margin="623,20,0,0" Grid.Row="1" VerticalAlignment="Top" Width="227" Height="34"/>
Please dont worry about datacontext everything is fine. I cant write the entire code of each class as it is so huge. Please Help!!!!