mirror of https://github.com/ogoun/Zero.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.8 KiB
69 lines
1.8 KiB
using System;
|
|
using System.Threading;
|
|
|
|
namespace ZeroLevel.Services.Applications
|
|
{
|
|
public abstract class BaseZeroService
|
|
: IZeroService
|
|
{
|
|
public string Name { get; protected set; }
|
|
public ZeroServiceState State => _state;
|
|
private ZeroServiceState _state;
|
|
|
|
protected BaseZeroService()
|
|
{
|
|
Name = GetType().Name;
|
|
}
|
|
|
|
protected BaseZeroService(string name)
|
|
{
|
|
Name = name;
|
|
}
|
|
|
|
private ManualResetEvent InteraciveModeWorkingFlag = new ManualResetEvent(false);
|
|
|
|
protected abstract void StartAction();
|
|
protected abstract void StopAction();
|
|
|
|
public void Start()
|
|
{
|
|
InteraciveModeWorkingFlag.Reset();
|
|
if (_state == ZeroServiceState.Initialized)
|
|
{
|
|
try
|
|
{
|
|
StartAction();
|
|
_state = ZeroServiceState.Started;
|
|
Log.Debug($"[{Name}] Service started");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Fatal(ex, $"[{Name}] Failed to start service");
|
|
InteraciveModeWorkingFlag.Set();
|
|
}
|
|
}
|
|
try
|
|
{
|
|
while (false == InteraciveModeWorkingFlag.WaitOne(2000))
|
|
{
|
|
}
|
|
}
|
|
catch { }
|
|
_state = ZeroServiceState.Stopped;
|
|
try
|
|
{
|
|
StopAction();
|
|
Log.Debug($"[{Name}] Service stopped");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Fatal(ex, $"[{Name}] Failed to stop service");
|
|
}
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
InteraciveModeWorkingFlag.Set();
|
|
}
|
|
}
|
|
} |