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


        }


    }

  • 相关阅读:
    李宏毅机器学习课程笔记-6.1神经网络训练问题与解决方案
    PAT甲级1055The World's Richest
    PAT甲级1028List Sorting
    PAT甲级1025PAT Ranking
    PAT甲级1016Phone Bills
    五边形数
    组合计数
    [编程题] lc三数之和 (借助哈希表)
    [编程题] 基础:如何使用大顶堆和小顶堆找topN
    [编程题] lc [剑指 Offer 54二叉搜索树的第k大节点----或者是求第K小元素]
  • 原文地址:https://www.cnblogs.com/kokoliu/p/1171621.html
Copyright © 2020-2023  润新知