zoukankan      html  css  js  c++  java
  • Timer 123

    1.System.Threading.Timer

    代码

    //http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx
    using System;
    using System.Threading;

    class TimerExample
    {
        
    static void Main()
        {
            
    // Create an event to signal the timeout count threshold in the
            
    // timer callback.
            AutoResetEvent autoEvent     = new AutoResetEvent(false);

            StatusChecker  statusChecker 
    = new StatusChecker(10);

            
    // Create an inferred delegate that invokes methods for the timer.
            TimerCallback tcb = statusChecker.CheckStatus;

            
    // Create a timer that signals the delegate to invoke 
            
    // CheckStatus after one second, and every 1/4 second 
            
    // thereafter.
            Console.WriteLine("{0} Creating timer.\n"
                DateTime.Now.ToString(
    "h:mm:ss.fff"));
            Timer stateTimer 
    = new Timer(tcb, autoEvent, 1000250);

            
    // When autoEvent signals, change the period to every
            
    // 1/2 second.
            autoEvent.WaitOne(5000false);
            stateTimer.Change(
    0500);
            Console.WriteLine(
    "\nChanging period.\n");

            
    // When autoEvent signals the second time, dispose of 
            
    // the timer.
            autoEvent.WaitOne(5000false);
            stateTimer.Dispose();
            Console.WriteLine(
    "\nDestroying timer.");
        }
    }

    class StatusChecker
    {
        
    private int invokeCount;
        
    private int  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();
            }
        }
    }
  • 相关阅读:
    C#操纵Excel,此工作薄包含嵌入对象,Office 2007的设定方法
    将本程序添加到自启动项
    .net表达式计算器(中缀表达式转后缀表达式,支持20多个数学函数,支持函数嵌套)
    Entity Framework 调用返回标量值的存储过程
    Dev控件 galleryControl
    Dev控件treeList
    Linux下USB驱动开发相关链接
    Linux下串口编程相关链接
    Linux常用命令操作说明(链接)
    Perl的主要应用领域
  • 原文地址:https://www.cnblogs.com/zyip/p/1862845.html
Copyright © 2011-2022 走看看