现在的位置: 首页 > 综合 > 正文

C# 定时器使用一例

2013年06月09日 ⁄ 综合 ⁄ 共 1205字 ⁄ 字号 评论关闭
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            AddRoomTimer timer 
= new AddRoomTimer();
            
while (true)
            
{
                
if (AddRoomTimer.COUNT > 0)
                
{
                    Console.WriteLine(
string.Format("第{0}次调用"101 - AddRoomTimer.COUNT));
                    AddRoomTimer.COUNT
--;
                }

            }


            
//System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);

            
//System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
        }

    }


    
public class AddRoomTimer
    
{
        
//静态计数器变量
        public static int COUNT = 0;
        public static System.Threading.Timer timer;

        //静态构造, 最多运行一次
        static AddRoomTimer()
        
{
            
//多线程定时器, 每隔 10000毫秒调用一次被委托的方法 TimerCallBack
            timer = new System.Threading.Timer(TimerCallBack, null010000);
        }


        
private static void TimerCallBack(object obj)
        
{
            
//打印一次时间, 测试用
            Console.WriteLine(DateTime.Now.ToString());
            
//计数器变量重置
            COUNT = 100;
        }

    }

}

抱歉!评论已关闭.