Skip to content

Commit

Permalink
add lock
Browse files Browse the repository at this point in the history
  • Loading branch information
Stéphane ANDRE (E104915) committed Sep 23, 2024
1 parent 6abcb9d commit 54a0da0
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions src/MyNet.Wpf/Controls/CalendarBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public abstract class CalendarBase : ListBox
private readonly SingleTaskDeferrer _refreshAppointments;
private readonly SingleTaskDeferrer _build;
private readonly Suspender _refreshAppointmentsSuspender = new();
private readonly object _lock = new();

protected Grid? Grid { get; private set; }

Expand Down Expand Up @@ -376,7 +377,7 @@ await Dispatcher.Invoke(() => BusyService).WaitAsync<IndeterminateBusy>(async _
{
foreach (var item in e.OldItems)
{
_appointments.RemoveMany(_appointments.Where(x => x.DataContext == item).ToList());
await RemoveAppointmentsAsync(_appointments.Where(x => x.DataContext == item).ToList(), CancellationToken.None).ConfigureAwait(false);
}
}

Expand Down Expand Up @@ -1814,21 +1815,47 @@ private async Task SynchronizeAppointmentAsync(IAppointment appointment, Cancell
if (AppointmentMustBeDisplayed(appointment))
appointmentsToAdd = CreateAppointmentsWrapper(appointment).ToList();

foreach (var item in appointmentsToDelete)
await RemoveAppointmentsAsync(appointmentsToDelete, cancellationToken).ConfigureAwait(false);

await AddAppointmentsAsync(appointmentsToAdd, cancellationToken).ConfigureAwait(false);
}

private async Task AddAppointmentsAsync(List<CalendarAppointment> appointmentsToAdd, CancellationToken cancellationToken)
{
foreach (var item in appointmentsToAdd)
{
cancellationToken.ThrowIfCancellationRequested();
_appointments.Remove(item);
AddAppointment(item);
await Task.Delay(1.Milliseconds(), cancellationToken).ConfigureAwait(false);
}
}

foreach (var item in appointmentsToAdd)
private async Task RemoveAppointmentsAsync(List<CalendarAppointment> appointmentsToDelete, CancellationToken cancellationToken)
{
foreach (var item in appointmentsToDelete)
{
cancellationToken.ThrowIfCancellationRequested();
_appointments.Add(item);
RemoveAppointment(item);
await Task.Delay(1.Milliseconds(), cancellationToken).ConfigureAwait(false);
}
}

private void RemoveAppointment(CalendarAppointment item)
{
lock (_lock)
{
_appointments.Remove(item);
}
}

private void AddAppointment(CalendarAppointment item)
{
lock (_lock)
{
_appointments.Add(item);
}
}

private bool AppointmentMustBeDisplayed(IAppointment appointment)
{
var period = new ImmutablePeriod(Dispatcher.Invoke(() => DisplayDateStart ?? DateTime.MinValue), Dispatcher.Invoke(() => DisplayDateEnd ?? DateTime.MaxValue));
Expand Down Expand Up @@ -1881,7 +1908,8 @@ private async Task RefreshAppointmentsAsync(CancellationToken cancellationToken)
{
try
{
_appointments.Clear();
lock (_lock)
_appointments.Clear();

var appointments = Dispatcher.Invoke(() => Appointments?.OfType<IAppointment>().ToList());

Expand Down

0 comments on commit 54a0da0

Please sign in to comment.