I am working on a WPF Prism
application. I have a DelgateCommand
that is responsible for populating an ObservableCollection
which is owned by the UI thread asynchronously using async and await
. The collection in turn is bound to a chart.
In order to enable the collection to be accessed by multiple threads
, I have enabled the synchronization
which is as follows:
BindingOperations.EnableCollectionSynchronization(ChartBoundCollection, _lock);
The command handler logic is as as follows:
private async void ShowPatientVisitsVsDays()
{
IsChartBeingPopulated = true;
this.ChartSubTitle = "Requests Vs Days";
this.SeriesTitle = "Days";
ChartBoundCollection.Clear();
await ShowRequestsVsDaysAsync();
IsChartBeingPopulated = false;
}
The Logic which populates the observable collection is as follows:
private async Task ShowRequestsVsDaysAsync()
{
await Task.Run(() =>
{
if (PatientVisits.Count() > 0)
{
var days = PatientVisits.Select(p => p.VisitDate.Value.Date).Distinct();
foreach (var i in days)
{
var dayVisitCount = PatientVisits.Where(p => p.VisitDate.Value.Date == i).Count();
ChartBoundCollection.Add(new PatientVisitVsXAxis() { XAxis = i.ToShortDateString(), NumberOfPatientVisits = dayVisitCount });
}
}
});
}
The issue that I am facing is that the continuation where I am setting IsChartBeingPopulated = false
is not getting executed after the asynchronous method on which the await is set is completed.
await ShowRequestsVsDaysAsync();
Thus IsChartBeingPopulated is set even before the asynchronous method is completed.
the command handler ShowPatientVisitsVsDays() is invoked by the click of the button on the View. The button is bound to the following command:
ShowPatientVisitsVsDaysCommand = new DelegateCommand(ShowPatientVisitsVsDays);
IsChartBeingPopulated
is being used to set the IsBusy DependencyProperty
of the BusyIndiator
control belonging to the 'Extended WPF ToolKit'.
The idea is to show the BusyIndicator
while the chart data is being populated in the bound collection.
<xctk:BusyIndicator IsBusy="{Binding Path=IsChartBeingPopulated}" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<chart:ClusteredColumnChart Grid.Row="0" ChartTitle="Patient Visits History" ChartSubTitle="{Binding Path=ChartSubTitle}">
<chart:ClusteredColumnChart.Series>
<chart:ChartSeries SeriesTitle="{Binding Path=SeriesTitle}" DisplayMember="XAxis" ValueMember="NumberOfPatientVisits" ItemsSource="{Binding Path=ChartBoundCollection}" />
</chart:ClusteredColumnChart.Series>
</chart:ClusteredColumnChart>
</Grid>
</xctk:BusyIndicator>
Not sure what the issue is. Does someone has any idea what is causing this?