//------------------------------------------------------------------------------ // 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有 // 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权 // CSDN博客:https://blog.csdn.net/qq_40374647 // 哔哩哔哩视频:https://space.bilibili.com/94253567 // Gitee源代码仓库:https://gitee.com/RRQM_Home // Github源代码仓库:https://github.com/RRQM // API首页:https://www.yuque.com/rrqm/touchsocket/index // 交流QQ群:234762506 // 感谢您的下载和使用 //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ using System; using System.Threading; using System.Threading.Tasks; namespace TouchSocket.Core { /// /// 循环动作 /// public class LoopAction : EasyTask, IDisposable { /// /// 析构函数 /// ~LoopAction() { Dispose(); } private int executedCount; private readonly TimeSpan m_interval; private readonly int m_loopCount; private readonly EventWaitHandle m_waitHandle; private LoopAction(int count, TimeSpan interval, Action action) { m_loopCount = count; this.action = action; m_interval = interval; m_waitHandle = new AutoResetEvent(false); } /// /// 创建可循环操作体 /// /// 循环次数,设为-1时一直循环 /// 每次循环间隔 /// 执行委托 /// public static LoopAction CreateLoopAction(int count, TimeSpan interval, Action action) { return new LoopAction(count, interval, action); } /// /// 创建可循环操作体 /// /// 循环次数,设为-1时一直循环 /// 每次循环间隔,毫秒 /// 执行委托 /// public static LoopAction CreateLoopAction(int count, int intervalMS, Action action) { return new LoopAction(count, TimeSpan.FromMilliseconds(intervalMS), action); } /// /// 创建可循环操作体 /// /// 循环次数,设为-1时一直循环 /// 执行委托 /// public static LoopAction CreateLoopAction(int count, Action action) { return CreateLoopAction(count, TimeSpan.Zero, action); } /// /// 创建可循环操作体 /// /// 每次循环间隔 /// 执行委托 /// public static LoopAction CreateLoopAction(TimeSpan interval, Action action) { return CreateLoopAction(-1, interval, action); } /// /// 创建可循环操作体 /// /// 执行委托 /// public static LoopAction CreateLoopAction(Action action) { return CreateLoopAction(-1, TimeSpan.Zero, action); } /// /// 已执行次数 /// public int ExecutedCount => executedCount; /// /// 执行间隔 /// public TimeSpan Interval => m_interval; /// /// 循环次数 /// public int LoopCount => m_loopCount; private readonly Action action; /// /// 执行委托 /// public Action ExecuteAction => action; private RunStatus runStatus; /// /// 是否在运行 /// public RunStatus RunStatus => runStatus; /// /// 运行 /// public void Run() { if (runStatus == RunStatus.None) { runStatus = RunStatus.Running; if (m_loopCount >= 0) { for (int i = 0; i < m_loopCount; i++) { if (runStatus == RunStatus.Disposed) { return; } action.Invoke(this); executedCount++; if (runStatus == RunStatus.Paused) { m_waitHandle.WaitOne(); } m_waitHandle.WaitOne(m_interval); } } else { while (true) { if (runStatus == RunStatus.Disposed) { return; } action.Invoke(this); executedCount++; if (runStatus == RunStatus.Paused) { m_waitHandle.WaitOne(); } m_waitHandle.WaitOne(m_interval); } } runStatus = RunStatus.Completed; } } /// /// 重新运行 /// public void Rerun() { if (runStatus == RunStatus.Disposed) { throw new Exception("无法利用已释放的资源"); } runStatus = RunStatus.None; Run(); } /// /// 以异步重新运行 /// /// public Task RerunAsync() { if (runStatus == RunStatus.Disposed) { throw new Exception("无法利用已释放的资源"); } runStatus = RunStatus.None; return RunAsync(); } /// /// 以异步运行 /// /// public Task RunAsync() { return EasyTask.Run(() => { Run(); }); } /// /// 暂停 /// public void Pause() { if (runStatus == RunStatus.Running) { m_waitHandle.Reset(); runStatus = RunStatus.Paused; } } /// /// 回复 /// public void Resume() { if (runStatus == RunStatus.Paused) { runStatus = RunStatus.Running; m_waitHandle.Set(); } } /// /// 释放资源 /// public void Dispose() { if (runStatus == RunStatus.Disposed) { return; } if (runStatus == RunStatus.Completed) { m_waitHandle.Dispose(); } runStatus = RunStatus.Disposed; } } }