小笔记: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; } }

 待完善。。。。。。

 

posted @ 2018-01-30 16:03  Johnny Li  阅读(2044)  评论(0编辑  收藏  举报