Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions Source/PeterKottas.DotNetCore.WindowsService/Base/Timer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,44 @@ public class Timer

public Action OnTimer { get; set; }

public Func<Task> OnTimerAsync { get; set; }

public Action<Exception> OnException { get; set; }

public string Name { get; private set; }

public int Interval { get; set; }

public Timer(string name, int interval, Func<Task> onTimerAsync, Action<Exception> onException = null)
{
this.OnTimer = null;
this.OnTimerAsync = onTimerAsync == null ? default(Func<Task>) : onTimerAsync; ;
this.Name = name;
this.Interval = interval;
this.OnException = onException == null ? (e) => { } : onException;
}

public Timer(string name, int interval, Action onTimer, Action<Exception> 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)
{
try
{
if (!paused)
{
this.OnTimer();
if (this.OnTimer != null)
this.OnTimer();
else
await this.OnTimerAsync();
}
}
catch (Exception exception)
Expand Down
38 changes: 38 additions & 0 deletions Source/PeterKottas.DotNetCore.WindowsService/Base/Timers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ public void Start(string timerName, int interval, Action onTimer, Action<Excepti
}
}

public void Start(string timerName, int interval, Func<Task> onTimer, Action<Exception> 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<Exception> onException = null)
{
var tmpTimer = timers.Where(x => x.Name == timerName).FirstOrDefault();
Expand All @@ -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<Task> onTimerAsync = null, Action<Exception> 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)
Expand Down