Compare commits
2 Commits
0ad2e86400
...
44e87f1334
| Author | SHA1 | Date | |
|---|---|---|---|
| 44e87f1334 | |||
| 61d43ef61e |
61
WarhoundConsole/Job.cs
Normal file
61
WarhoundConsole/Job.cs
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
namespace WarhoundConsole
|
||||||
|
{
|
||||||
|
public abstract class Job
|
||||||
|
{
|
||||||
|
private readonly TimeSpan interval;
|
||||||
|
|
||||||
|
private Task task;
|
||||||
|
private DateTime lastExecutedTime;
|
||||||
|
|
||||||
|
public Job(TimeSpan interval)
|
||||||
|
{
|
||||||
|
if (interval <= TimeSpan.Zero)
|
||||||
|
{
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(interval));
|
||||||
|
}
|
||||||
|
|
||||||
|
this.interval = interval;
|
||||||
|
this.task = Task.CompletedTask;
|
||||||
|
this.lastExecutedTime = DateTime.MinValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryExecute()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var now = DateTime.Now;
|
||||||
|
if (now - this.lastExecutedTime < this.interval)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.task.IsCompleted)
|
||||||
|
{
|
||||||
|
// 이 경우 로그를 출력하도록 하고, 타이밍을 조절해야한다.
|
||||||
|
//Console.WriteLine($"Job skipped at {DateTime.Now}");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.lastExecutedTime = now;
|
||||||
|
|
||||||
|
this.task = Task.Run(async () =>
|
||||||
|
{
|
||||||
|
await this.Execute();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Console.WriteLine(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract Task Execute();
|
||||||
|
|
||||||
|
public void Wait()
|
||||||
|
{
|
||||||
|
this.task?.Wait();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
namespace WarhoundConsole
|
namespace WarhoundConsole
|
||||||
{
|
{
|
||||||
public class Program
|
public class Program
|
||||||
@@ -22,15 +24,43 @@ namespace WarhoundConsole
|
|||||||
{
|
{
|
||||||
Singleton.I.Initialize(new());
|
Singleton.I.Initialize(new());
|
||||||
|
|
||||||
bool exitRequested = Singleton.I.ExitRequested;
|
var jobs = new List<Job>();
|
||||||
while (!exitRequested)
|
|
||||||
|
var sw = Stopwatch.StartNew();
|
||||||
|
while (!Singleton.I.ExitRequested)
|
||||||
{
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
sw.Restart();
|
||||||
|
foreach (var job in jobs)
|
||||||
|
{
|
||||||
|
_ = job.TryExecute();
|
||||||
|
}
|
||||||
|
sw.Stop();
|
||||||
|
|
||||||
|
const int targetFrameTimeMs = 10;
|
||||||
exitRequested = Singleton.I.ExitRequested;
|
if (sw.Elapsed.Milliseconds < targetFrameTimeMs)
|
||||||
|
{
|
||||||
|
Thread.Sleep(targetFrameTimeMs - sw.Elapsed.Milliseconds);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Console.WriteLine(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Graceful shutdown
|
foreach (var job in jobs)
|
||||||
|
{
|
||||||
|
job.Wait();
|
||||||
|
}
|
||||||
|
|
||||||
|
Shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void Shutdown()
|
||||||
|
{
|
||||||
|
// DO SOMETHING
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user