• 小笔记:Timer定时间隔时间操作


    小笔记:Timer定时间隔时间操作,后面有时间再补充和完善:

    public class TimingSvc
        {
            /// <summary>
            /// 定时器,执行定时任务
            /// </summary>
            private static Timer m_Timer;
            static ReaderWriterLockSlim rwLock = new ReaderWriterLockSlim();
    private static int FlushTimeOut = 5000;
    private TimingSvc() { //初始化操作 TimeSpan timeSpan = new TimeSpan(20000); DateTime nextTime = DateTime.Now.AddMilliseconds(20000); timeSpan = nextTime.Subtract(DateTime.Now); m_Timer = new Timer(new TimerCallback(OnTimedEvent), null, (long)timeSpan.TotalMilliseconds, Timeout.Infinite); } public void Close() { m_log.LogEvent("定时任务停止."); m_Timer.Dispose(); } /// <summary> /// 定时任务的执行 /// </summary> protected void OnTimedEvent(object state) { //LogEvent("********开始执行定时任务********"); //开始执行,无限等待 m_Timer.Change(Timeout.Infinite, Timeout.Infinite); //执行任务逻辑,刷新数据 //TODO //下次间隔20s执行 DateTime nextTime = DateTime.Now.AddMilliseconds(20000); TimeSpan timeSpan = nextTime.Subtract(DateTime.Now); m_Timer.Change((long)timeSpan.TotalMilliseconds, Timeout.Infinite); return; } public void DoOperation() { //写入加锁 if (rwLock.TryEnterWriteLock(FlushTimeOut)) { try { //TODO BLL } catch (Exception ex) { LogError("赋值异常:" + ex.Message); } finally { rwLock.ExitWriteLock(); } } return; } }

     待完善。。。。。。

  • 相关阅读:
    ($children,$refs,$parent)的使用
    watch监听变化
    vue组件间通信六种方式(完整版)
    CSS水平垂直居中常见方法总结2
    CSS水平垂直居中常见方法总结
    Uncaught SyntaxError: Unexpected identifier 报错 import Vue from 'vue';
    前端跳槽面试必备
    防止重复发送Ajax请求问题
    JQuery中的$().each 以及 $.each的区别
    数组中的forEach和map的区别
  • 原文地址:https://www.cnblogs.com/lisweden/p/8385490.html
Copyright © 2020-2023  润新知