From 053928363bd2f984eba18344b8de843a949ce856 Mon Sep 17 00:00:00 2001 From: Sebastian B Date: Fri, 14 Sep 2018 14:22:47 +0200 Subject: [PATCH] Allow onException timer handler to catch exceptions thrown in async methods --- .../Base/Timer.cs | 19 +++++++++- .../Base/Timers.cs | 38 +++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/Source/PeterKottas.DotNetCore.WindowsService/Base/Timer.cs b/Source/PeterKottas.DotNetCore.WindowsService/Base/Timer.cs index 5473ea3..58d8cdf 100644 --- a/Source/PeterKottas.DotNetCore.WindowsService/Base/Timer.cs +++ b/Source/PeterKottas.DotNetCore.WindowsService/Base/Timer.cs @@ -18,21 +18,33 @@ public class Timer public Action OnTimer { get; set; } + public Func OnTimerAsync { get; set; } + public Action OnException { get; set; } public string Name { get; private set; } public int Interval { get; set; } + public Timer(string name, int interval, Func onTimerAsync, Action onException = null) + { + this.OnTimer = null; + this.OnTimerAsync = onTimerAsync == null ? default(Func) : onTimerAsync; ; + this.Name = name; + this.Interval = interval; + this.OnException = onException == null ? (e) => { } : onException; + } + public Timer(string name, int interval, Action onTimer, Action onException = null) { this.OnTimer = onTimer == null ? () => { } : onTimer; ; + this.OnTimerAsync = null; this.Name = name; this.Interval = interval; this.OnException = onException == null ? (e) => { } : onException; } - private void InternalWork(object arg) + private async void InternalWork(object arg) { while (running) { @@ -40,7 +52,10 @@ private void InternalWork(object arg) { if (!paused) { - this.OnTimer(); + if (this.OnTimer != null) + this.OnTimer(); + else + await this.OnTimerAsync(); } } catch (Exception exception) diff --git a/Source/PeterKottas.DotNetCore.WindowsService/Base/Timers.cs b/Source/PeterKottas.DotNetCore.WindowsService/Base/Timers.cs index 80899e5..473e4f0 100644 --- a/Source/PeterKottas.DotNetCore.WindowsService/Base/Timers.cs +++ b/Source/PeterKottas.DotNetCore.WindowsService/Base/Timers.cs @@ -27,6 +27,24 @@ public void Start(string timerName, int interval, Action onTimer, Action onTimer, Action onException = null) + { + var tmpTimer = timers.Where(x => x.Name == timerName).FirstOrDefault(); + if (tmpTimer == null) + { + tmpTimer = new Timer(timerName, interval, onTimer, onException); + timers.Add(tmpTimer); + + tmpTimer.Start(); + } + else + { + tmpTimer.Stop(); + Update(timerName, interval, onTimer, onException); + tmpTimer.Start(); + } + } + public void Update(string timerName, int interval = 0, Action onTimer = null, Action onException = null) { var tmpTimer = timers.Where(x => x.Name == timerName).FirstOrDefault(); @@ -47,6 +65,26 @@ public void Update(string timerName, int interval = 0, Action onTimer = null, Ac } } + public void Update(string timerName, int interval = 0, Func onTimerAsync = null, Action onException = null) + { + var tmpTimer = timers.Where(x => x.Name == timerName).FirstOrDefault(); + if (tmpTimer != null) + { + if (onTimerAsync != null) + { + tmpTimer.OnTimerAsync = onTimerAsync; + } + if (onException != null) + { + tmpTimer.OnException = onException; + } + if (interval > 0 && interval != tmpTimer.Interval) + { + tmpTimer.Interval = interval; + } + } + } + public void Resume() { foreach (var timer in timers)