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 _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 Ended { get => _ended; } #endregion public CronInstance() { _thread = new Thread(Listen); _thread.IsBackground = true; } /// 打断循环。 public void Break() => _break = true; /// 启动线程执行任务。 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); } }