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
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,45 @@ namespace PeterKottas.DotNetCore.WindowsService.Example
{
public class ExampleService : IMicroService
{
private IMicroServiceController controller;
private IMicroServiceController _controller;

private Timer timer = new Timer(1000);
private readonly Timer _timer = new Timer(1000);
private readonly string _fileName = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "log.txt");

public ExampleService()
{
controller = null;
_controller = null;
}

public ExampleService(IMicroServiceController controller)
{
this.controller = controller;
_controller = controller;
}

private string fileName = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "log.txt");
public void Start()
{
Console.WriteLine("I started");
Console.WriteLine(fileName);
File.AppendAllText(fileName, "Started\n");
Console.WriteLine(_fileName);
File.AppendAllText(_fileName, "Started\n");

/**
* A timer is a simple example. But this could easily
* be a port or messaging queue client
*/
timer.Elapsed += _timer_Elapsed;
timer.Start();
_timer.Elapsed += _timer_Elapsed;
_timer.Start();
}

private void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
File.AppendAllText(fileName, string.Format("Polling at {0}\n", DateTime.Now.ToString("o")));
}

public void Stop()
{
timer.Stop();
File.AppendAllText(fileName, "Stopped\n");
_timer.Stop();
File.AppendAllText(_fileName, "Stopped\n");
Console.WriteLine("I stopped");
}

private void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
File.AppendAllText(_fileName, string.Format("Polling at {0}\n", DateTime.Now.ToString("o")));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace PeterKottas.DotNetCore.WindowsService.Example
public class ExampleServiceTimer : MicroService, IMicroService
{
private IMicroServiceController controller;
private readonly string _fileName = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "log.txt");

public ExampleServiceTimer()
{
Expand All @@ -20,22 +21,21 @@ public ExampleServiceTimer(IMicroServiceController controller)
this.controller = controller;
}

private string fileName = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "log.txt");
public void Start()
{
StartBase();
Timers.Start("Poller", 1000, () =>
{
File.AppendAllText(fileName, string.Format("Polling at {0}\n", DateTime.Now.ToString("o")));
File.AppendAllText(_fileName, string.Format("Polling at {0}\n", DateTime.Now.ToString("o")));
});
Console.WriteLine("I started");
File.AppendAllText(fileName, "Started\n");
File.AppendAllText(_fileName, "Started\n");
}

public void Stop()
{
StopBase();
File.AppendAllText(fileName, "Stopped\n");
File.AppendAllText(_fileName, "Stopped\n");
Console.WriteLine("I stopped");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
using PeterKottas.DotNetCore.WindowsService.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System;

namespace PeterKottas.DotNetCore.WindowsService.Base
{
public class MicroService : IDisposable
{
protected Timers Timers { get; private set; }
private bool disposed = false;
private bool _disposed;

public void StartBase()
{
Expand All @@ -32,15 +28,15 @@ public void Dispose()

protected virtual void Dispose(bool disposing)
{
if (disposed)
if (_disposed)
return;

if (disposing)
{
StopBase();
}

disposed = true;
_disposed = true;
}

~MicroService()
Expand Down
61 changes: 29 additions & 32 deletions Source/PeterKottas.DotNetCore.WindowsService/Base/Timer.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace PeterKottas.DotNetCore.WindowsService.Base
{
Expand All @@ -11,10 +8,10 @@ namespace PeterKottas.DotNetCore.WindowsService.Base
/// </summary>
public class Timer
{
private Thread thread;
private AutoResetEvent stopRequest;
private bool running = true;
private bool paused = false;
private Thread _thread;
private AutoResetEvent _stopRequest;
private bool _running = true;
private bool _paused;

public Action OnTimer { get; set; }

Expand All @@ -26,73 +23,73 @@ public class Timer

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

private void InternalWork(object arg)
{
while (running)
while (_running)
{
try
{
if (!paused)
if (!_paused)
{
this.OnTimer();
OnTimer();
}
}
catch (Exception exception)
{
this.OnException(exception);
OnException(exception);
}

try
{
if (stopRequest.WaitOne(Interval))
if (_stopRequest.WaitOne(Interval))
{
return;
}
}
catch (Exception exception)
{
this.OnException(exception);
OnException(exception);
}

}
}

public void Start()
{
stopRequest = new AutoResetEvent(false);
running = true;
thread = new Thread(InternalWork);
thread.Start();
_stopRequest = new AutoResetEvent(false);
_running = true;
_thread = new Thread(InternalWork);
_thread.Start();
}

public void Pause()
{
paused = true;
_paused = true;
}

public void Resume()
{
paused = false;
_paused = false;
}

public void Stop()
{
if (running)
{
running = false;
stopRequest.Set();
thread.Join();
if (!_running)
return;

thread = null;
stopRequest.Dispose();
stopRequest = null;
}
_running = false;
_stopRequest.Set();
_thread.Join();

_thread = null;
_stopRequest.Dispose();
_stopRequest = null;
}
}
}
52 changes: 28 additions & 24 deletions Source/PeterKottas.DotNetCore.WindowsService/Base/Timers.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PeterKottas.DotNetCore.WindowsService.Base
{
public class Timers
{
List<Timer> timers = new List<Timer>();
private readonly List<Timer> _timers = new List<Timer>();

public void Start(string timerName, int interval, Action onTimer, Action<Exception> onException = null)
{
var tmpTimer = timers.Where(x => x.Name == timerName).FirstOrDefault();
var tmpTimer = _timers.FirstOrDefault(x => x.Name == timerName);

if (tmpTimer == null)
{
tmpTimer = new Timer(timerName, interval, onTimer, onException);
timers.Add(tmpTimer);
_timers.Add(tmpTimer);

tmpTimer.Start();
}
Expand All @@ -29,35 +29,37 @@ public void Start(string timerName, int interval, Action onTimer, Action<Excepti

public void Update(string timerName, int interval = 0, Action onTimer = null, Action<Exception> onException = null)
{
var tmpTimer = timers.Where(x => x.Name == timerName).FirstOrDefault();
if (tmpTimer != null)
var tmpTimer = _timers.FirstOrDefault(x => x.Name == timerName);

if (tmpTimer == null)
return;

if (onTimer != null)
{
tmpTimer.OnTimer = onTimer;
}
if (onException != null)
{
tmpTimer.OnException = onException;
}
if (interval > 0 && interval != tmpTimer.Interval)
{
if (onTimer != null)
{
tmpTimer.OnTimer = onTimer;
}
if (onException != null)
{
tmpTimer.OnException = onException;
}
if (interval > 0 && interval != tmpTimer.Interval)
{
tmpTimer.Interval = interval;
}
tmpTimer.Interval = interval;
}
}

public void Resume()
{
foreach (var timer in timers)
foreach (var timer in _timers)
{
timer.Resume();
}
}

public void Resume(string timerName)
{
var tmpTimer = timers.Where(x => x.Name == timerName).FirstOrDefault();
var tmpTimer = _timers.FirstOrDefault(x => x.Name == timerName);

if (tmpTimer != null)
{
tmpTimer.Resume();
Expand All @@ -66,15 +68,16 @@ public void Resume(string timerName)

public void Pause()
{
foreach (var timer in timers)
foreach (var timer in _timers)
{
timer.Pause();
}
}

public void Pause(string timerName)
{
var tmpTimer = timers.Where(x => x.Name == timerName).FirstOrDefault();
var tmpTimer = _timers.FirstOrDefault(x => x.Name == timerName);

if (tmpTimer != null)
{
tmpTimer.Pause();
Expand All @@ -83,15 +86,16 @@ public void Pause(string timerName)

public void Stop()
{
foreach (var timer in timers)
foreach (var timer in _timers)
{
timer.Stop();
}
}

public void Stop(string timerName)
{
var tmpTimer = timers.Where(x => x.Name == timerName).FirstOrDefault();
var tmpTimer = _timers.FirstOrDefault(x => x.Name == timerName);

if (tmpTimer != null)
{
tmpTimer.Stop();
Expand Down
Loading