From a9b54da04e9b032802c0c850bb0873cfd7ca2542 Mon Sep 17 00:00:00 2001 From: mary-dcouto Date: Mon, 5 Jul 2021 11:27:09 +0530 Subject: [PATCH] adding custom timer --- ZitiUpdateService/utils/CustomTimer.cs | 43 ++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 ZitiUpdateService/utils/CustomTimer.cs diff --git a/ZitiUpdateService/utils/CustomTimer.cs b/ZitiUpdateService/utils/CustomTimer.cs new file mode 100644 index 00000000..799fc5cf --- /dev/null +++ b/ZitiUpdateService/utils/CustomTimer.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace ZitiUpdateService.Utils { + class CustomTimer : IDisposable { + private readonly TimerCallback _realCallback; + private readonly Timer _timer; + private TimeSpan _period; + private DateTime _next; + + public CustomTimer(TimerCallback callback, object state, TimeSpan dueTime, TimeSpan period) { + _timer = new Timer(Callback, state, dueTime, period); + _realCallback = callback; + _period = period; + _next = DateTime.Now.Add(dueTime); + } + + private void Callback(object state) { + _next = DateTime.Now.Add(_period); + _realCallback(state); + } + + public TimeSpan Period => _period; + public DateTime Next => _next; + public TimeSpan DueTime => _next - DateTime.Now; + public bool Change(TimeSpan dueTime, TimeSpan period) { + _period = period; + _next = DateTime.Now.Add(dueTime); + return _timer.Change(dueTime, period); + } + + public void Dispose() => _timer.Dispose(); + } + + class TimerState { + public CustomTimer _timer; + public Checkers.ZDEInstallerInfo zdeInstallerInfo; + } +}