唐朝程序员

我来自唐朝

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


    }


}

posted on 2008-04-25 22:25  唐朝程序员  阅读(833)  评论(0编辑  收藏  举报

导航