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();
                
    //}
            }


        }


    }

  • 相关阅读:
    TCP/IP资料整理
    对谷歌自带闹钟应用中材料设计的吹毛求疵
    如何给非AppCompatActivity添加Toolbar?--关于5.0新特性兼容5.0以下设备的探索
    Android 5.0自定义动画
    Android开发Tips-1
    Android控件RecyclerView与ListView的异同
    有关Android存储的相关概念辨析
    关于RecyclerView中Viewholder和View的缓存机制的探究
    Android Wear和二维码
    构建具有深度和灵活性的安卓Wear应用
  • 原文地址:https://www.cnblogs.com/kokoliu/p/1171621.html
Copyright © 2011-2022 走看看