System.Timers.Timer 与 System.Threading.Timer 小间隔

设计一个每隔20ms检查一次状态的程序,用 System.Timers.Timer做测试时发现,前几次执行timer调用函数的时间相同(把间隔改到1s以上时无此问题),用lock也用(可能是我不太会用lock)。
改为System.Threading.Timer测试发现:
Threading和Timers的timer在小间隔时都存在此问题,分析后初步判断是初次运行前的间隔时间的问题。
Timers的无法设置初次启动前间隔所以设置20ms间隔时第一次进入前的间隔也是20ms
Threading的可以设置初次启动前的间隔,设置较大间隔后启动,便没有了初次运行时多线程同时进入的情况。

timerClose = new System.Threading.Timer(new TimerCallback(timerCall), null , 20, 20);//多次进入
timerClose = new System.Threading.Timer(new TimerCallback(timerCall), null , 1000, 20);//正常进入



测试用的代码:
using  System;
using  System.Collections.Generic;
using  System.Windows.Forms;
using  System.Timers;
using  System.Threading;

namespace  WindowsApplication3
{
    
static class Program
    
{
        
/**//// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main()
        
{
            
//System.Threading.Timer thrTimer = new System.Threading.Timer();

            
            

            System.Timers.Timer timer 
= new System.Timers.Timer();
            timer.Elapsed 
+= new ElapsedEventHandler(timer_Elapsed);

            timer.AutoReset 
= true;
            timer.Interval 
=20;

            System.Threading.Timer timerClose;
            
//解决初次进入timer调用函数时多线程同时访问函数的问题
            
//Threading和Timers的timer都存在此问题分析后初步判断是初次运行前的间隔时间的问题
            
//Timers的无法设置初次启动前间隔所以设置20ms间隔时第一次进入前的间隔也是20ms
            
//Threading的可以设置初次启动前的间隔,设置较大间隔后启动,便没有了初次运行时多线程同时进入的情况。
            
//timerClose = new System.Threading.Timer(new TimerCallback(timerCall), null , 1000, 20);
            
//timerClose = new System.Threading.Timer(new TimerCallback(timerCall), null , 20, 20);
            
//timer.Start();


            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(
false);
            Application.Run(
new Form1());
        }

        
public static  void timer_Elapsed(object sender, ElapsedEventArgs e)
        
{
            Console.Out.WriteLine(
"system   :"+DateTime.Now + " " + DateTime.Now.Millisecond + " "+DateTime .Now.TimeOfDay.TotalMilliseconds );

        }

        
//object oo = new object();
        
//static int inTimer = 0; 
        public static void timerCall(object obj)
        
{

            
//timerClose.Dispose();
            
//lock (this)
            
//if (Interlocked.Exchange(ref inTimer, 1) == 0) 
            {
                
//Console.Out.WriteLine(Environment.TickCount);
                Console.Out.WriteLine("Threading:" + DateTime.Now + " " + DateTime.Now.Millisecond + " " + DateTime.Now.TimeOfDay.TotalMilliseconds);
                
//this.Close();
                
//Interlocked.Exchange(ref inTimer, 0); 
            }


        }





    }

}

posted on 2008-05-09 11:08 wills 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/fchen/archive/2008/05/09/1189633.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值