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.

95 lines
2.4 KiB

using Apewer;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace Apewer.Web
{
internal sealed class CronInstance
{
internal bool _latest = false;
internal Type _type = null;
internal Logger _logger = null;
internal Class<DateTime> _ended = null;
internal CronAttribute _attribute = null;
internal CronInvoker _invoker = null;
private Thread _thread = null;
private bool _break = false;
#region properties
// 当前线程正在运行。
public bool Alive { get => GetAlive(); }
// 再次启动 Cron 的时间间隔。
public int Interval { get => GetInterval(); }
// 最后一次检查的 Alive 值。
public bool Latest { get => _latest; }
// Cron 类型。
public Type Type { get => _type; }
public CronAttribute Attribute { get => _attribute; }
public Class<DateTime> Ended { get => _ended; }
#endregion
public CronInstance()
{
_thread = new Thread(Listen);
_thread.IsBackground = true;
}
/// <summary>打断循环。</summary>
public void Break() => _break = true;
/// <summary>启动线程执行任务。</summary>
public void Start()
{
if (Alive) return;
_thread = new Thread(Listen);
_thread.IsBackground = true;
_thread.Start();
}
int GetInterval()
{
if (_attribute == null) _attribute = new CronAttribute();
return _attribute.Interval;
}
bool GetAlive()
{
if (_thread == null) return false;
if (_thread.IsAlive != true) return false;
if (_thread.ThreadState != ThreadState.Running) return false;
return true;
}
void Listen()
{
if (Type == null) return;
var instance = null as object;
try
{
instance = Activator.CreateInstance(Type);
}
catch (Exception exception)
{
Log(Type.FullName, exception.GetType().FullName, exception.Message);
}
RuntimeUtility.Dispose(instance);
_thread = null;
}
void Log(params object[] content) => _logger.Text(Type.FullName, content);
}
}