|
@ -194,6 +194,7 @@ namespace Apewer |
|
|
static CronAttribute[] _crons = null; |
|
|
static CronAttribute[] _crons = null; |
|
|
static bool _break = false; |
|
|
static bool _break = false; |
|
|
static DateTime _now; |
|
|
static DateTime _now; |
|
|
|
|
|
static int _crons_alive = 0; |
|
|
|
|
|
|
|
|
static CronAttribute[] Init(IEnumerable<Assembly> assemblies, Logger logger, bool logEvent) |
|
|
static CronAttribute[] Init(IEnumerable<Assembly> assemblies, Logger logger, bool logEvent) |
|
|
{ |
|
|
{ |
|
@ -250,6 +251,7 @@ namespace Apewer |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 启动线程。
|
|
|
// 启动线程。
|
|
|
|
|
|
_crons_alive = _crons.Length; |
|
|
Console.CancelKeyPress += (s, e) => |
|
|
Console.CancelKeyPress += (s, e) => |
|
|
{ |
|
|
{ |
|
|
_break = true; |
|
|
_break = true; |
|
@ -261,22 +263,64 @@ namespace Apewer |
|
|
// 监视退出状态。
|
|
|
// 监视退出状态。
|
|
|
while (true) |
|
|
while (true) |
|
|
{ |
|
|
{ |
|
|
Thread.Sleep(300); |
|
|
Thread.Sleep(100); |
|
|
_now = DateTime.Now; |
|
|
_now = DateTime.Now; |
|
|
var alive = 0; |
|
|
var alive = 0; |
|
|
for (var i = 0; i < _crons.Length; i++) if (_crons[i]._alive) alive += 1; |
|
|
for (var i = 0; i < _crons.Length; i++) if (_crons[i]._alive) alive += 1; |
|
|
if (alive < 1) break; |
|
|
if (alive < 1) break; |
|
|
|
|
|
_crons_alive = alive; |
|
|
} |
|
|
} |
|
|
logger.Text(nameof(CronAttribute), "所有 Cron 已结束。"); |
|
|
logger.Text(nameof(CronAttribute), "所有 Cron 已结束。"); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>打断正在执行的 Cron。</summary>
|
|
|
|
|
|
public static void Abort() |
|
|
|
|
|
{ |
|
|
|
|
|
var crons = _crons; |
|
|
|
|
|
if (crons != null) |
|
|
|
|
|
{ |
|
|
|
|
|
foreach (var cron in crons) |
|
|
|
|
|
{ |
|
|
|
|
|
if (cron == null) continue; |
|
|
|
|
|
try { cron._thread.Abort(); } catch { } |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>打断 Cron 循环,不打断正在执行的 Cron。</summary>
|
|
|
/// <summary>打断 Cron 循环,不打断正在执行的 Cron。</summary>
|
|
|
public static void Break() |
|
|
public static void Break() |
|
|
{ |
|
|
{ |
|
|
_break = true; |
|
|
_break = true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>打断 Cron 循环并等待 Cron 执行结束。等待指定的时间后打断正在执行的 Cron。</summary>
|
|
|
|
|
|
/// <param name="timeout">强制打断前的等待毫秒数,指定为负数时将无限等待。</param>
|
|
|
|
|
|
public static void Break(int timeout = 20000) |
|
|
|
|
|
{ |
|
|
|
|
|
_break = true; |
|
|
|
|
|
|
|
|
|
|
|
const int interval = 100; |
|
|
|
|
|
if (timeout < 0) |
|
|
|
|
|
{ |
|
|
|
|
|
while (_crons_alive > 0) Thread.Sleep(interval); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
if (timeout > 0) |
|
|
|
|
|
{ |
|
|
|
|
|
var remains = timeout; |
|
|
|
|
|
while (remains > 0 && _crons_alive > 0) |
|
|
|
|
|
{ |
|
|
|
|
|
Thread.Sleep(interval); |
|
|
|
|
|
remains -= interval; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
Abort(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>获取状态,指示打断 Cron 循环。</summary>
|
|
|
/// <summary>获取状态,指示打断 Cron 循环。</summary>
|
|
|
public static bool Breaking { get => _break; } |
|
|
public static bool Breaking { get => _break; } |
|
|
|
|
|
|
|
|