From adfb4820729436b707d582bcaf78caf147952df4 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sat, 22 Dec 2018 16:05:45 +0000 Subject: [PATCH] Bugfix: Notify the reminder thread when a reminder gets deleted from the list --- RhinoReminds/ClientListener.cs | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/RhinoReminds/ClientListener.cs b/RhinoReminds/ClientListener.cs index f51b7da..eb8252b 100644 --- a/RhinoReminds/ClientListener.cs +++ b/RhinoReminds/ClientListener.cs @@ -33,7 +33,8 @@ namespace RhinoReminds public string ReminderFilePath { get; set; } = "./reminders.xml"; private ReminderList reminderList = new ReminderList(); - + private CancellationTokenSource reminderWatcherReset; + private CancellationToken reminderWatcherResetToken; private SimpleXmppClient client; /// @@ -224,6 +225,8 @@ namespace RhinoReminds client.SendChatReply(message, response); } if (succeeded.Count > 0) { + // Ensure that the reminder thread picks up the changes + resetReminderWatcher(); string response = string.Join(", ", succeeded.Select((int nextId) => $"#{nextId}")); response = $"Deleted reminder{(succeeded.Count != 1 ? "s" : "")} {response} successfully."; client.SendChatReply(message, response); @@ -299,10 +302,16 @@ namespace RhinoReminds #region Reminder Listening + private void resetReminderWatcher() + { + // We don't need to create a new token here, as the watcher does thisi automagically + reminderWatcherReset.Cancel(); + } + private async Task watchForReminders() { - CancellationTokenSource cancellationSource = new CancellationTokenSource(); - CancellationToken cancellationToken = cancellationSource.Token; + reminderWatcherReset = new CancellationTokenSource(); + reminderWatcherResetToken = reminderWatcherReset.Token; Reminder nextReminder = reminderList.GetNextReminder(); reminderList.OnReminderListUpdate += (object sender, Reminder newReminder) => { Reminder newNextReminder = reminderList.GetNextReminder(); @@ -311,7 +320,7 @@ namespace RhinoReminds if (nextReminder != newNextReminder) { //Console.WriteLine($"[Rhino/Reminderd/Canceller] Cancelling"); nextReminder = newNextReminder; - cancellationSource.Cancel(); + reminderWatcherReset.Cancel(); } }; @@ -323,23 +332,23 @@ namespace RhinoReminds nextWaitingTime = nextReminder.Time - DateTime.Now; if (DateTime.Now < nextReminder.Time) { //Console.WriteLine($"[Rhino/Reminderd] Sleeping for {nextWaitingTime}"); - await Task.Delay(nextWaitingTime, cancellationToken); + await Task.Delay(nextWaitingTime, reminderWatcherResetToken); } } else { //Console.WriteLine("[Rhino/Reminderd] Sleeping until interrupted"); - await Task.Delay(Timeout.Infinite, cancellationToken); + await Task.Delay(Timeout.Infinite, reminderWatcherResetToken); } } catch (TaskCanceledException) { //Console.WriteLine("[Rhino/Reminderd] Sleep interrupted, recalculating"); - cancellationSource = new CancellationTokenSource(); - cancellationToken = cancellationSource.Token; + reminderWatcherReset = new CancellationTokenSource(); + reminderWatcherResetToken = reminderWatcherReset.Token; continue; } - if (cancellationToken.IsCancellationRequested) { + if (reminderWatcherResetToken.IsCancellationRequested) { Console.WriteLine("[Rhino/Reminderd] Sleep interrupted, recalculating (but no exception thrown)"); - cancellationSource = new CancellationTokenSource(); - cancellationToken = cancellationSource.Token; + reminderWatcherReset = new CancellationTokenSource(); + reminderWatcherResetToken = reminderWatcherReset.Token; continue; }