zoukankan      html  css  js  c++  java
  • System.Threading.TimerCallback 永不停止的定时器

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;

    namespace ConsoleApplication1
    {
        
    class TimerExample
        
    {
            
    static void Main(string[] args)
            
    {
                AutoResetEvent autoEvent 
    = new AutoResetEvent(false);
                StatusChecker statusChecker 
    = new StatusChecker(10);

                TimerCallback timerDelegate 
    = new TimerCallback(statusChecker.CheckStatus);

                Console.WriteLine(
    "服务器开始扫描:" +
                DateTime.Now.ToString(
    "h:mm:ss.fff"));
                Timer stateTimer 
    =
                
    new Timer(timerDelegate, autoEvent, 10005000);

                autoEvent.WaitOne(
    -1false);

                stateTimer.Dispose();
                Console.WriteLine(
    "xxxxxxxx."); 


            }



          

        }

        
    class StatusChecker
        
    {
            
    int invokeCount, maxCount;

            
    public StatusChecker(int count)
            
    {
                invokeCount 
    = 0;
                maxCount 
    = count;
            }


            
    // This method is called by the timer delegate.
            public void CheckStatus(Object stateInfo)
            
    {
                
    //AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;

               
                Console.WriteLine(
    "{0} Checking status {1,2}.",
                    DateTime.Now.ToString(
    "h:mm:ss.fff"),
                    (
    ++invokeCount).ToString());

                
    //if (invokeCount == maxCount)
                
    //{
                
    //    // Reset the counter and signal Main.
                
    //    invokeCount = 0;
                
    //    autoEvent.Set();
                
    //}
            }


        }


    }

  • 相关阅读:
    【python3的进阶之路一】正则表达式
    基础编程练习50道
    【python3的学习之路十四】IO编程
    【python3的学习之路十三】错误和调试
    【python3的学习之路十二】面向对象高级编程
    【python3的学习之路十一】面向对象编程
    jQuery之防止冒泡事件,冒泡事件就是点击子节点,会向上触发父节点,祖先节点的点击事件。
    手机移动端WEB资源整合
    js 验证表单 js提交验证类
    js单条新闻向上滚动
  • 原文地址:https://www.cnblogs.com/kokoliu/p/1171621.html
Copyright © 2011-2022 走看看