在.Net中有三种Timer分别是
private System.Windows.Forms.Timer
private System.Timers.Timer
private System.Threading.Timer
今天做了个实验,发现这三种Timer最终最小定时间隔都只有15-16毫秒,就是说把Timer得Interval间隔设成1-16,那么得到得效果将会是一样得,当然实验并不严谨,不过在普通应用中,我们可以认为.Net得这三个Timer最小间隔为20毫秒
实验环境如下:
硬件环境:CPU P4 2.4 内存1G
操作系统:Windows Server 2003 Enterprise Edition SP2
开发环境:Visual Studio 2005
代码片段:
private
System.Windows.Forms.Timer T1
private System.Timers.Timer T2
private System.Threading.Timer T3
ArrayList al1 = new ArrayList();
ArrayList al2 = new ArrayList();
ArrayList al3 = new ArrayList();
private void btnStart_Click( object sender,EventArgs e)
... {
t1.Interval = 1;
t1.Tick += new EventHandler(t1_Tick);
t1.Enabled = true;
t2.Interval = 1;
t2.Elapsed += new System.Timers.ElapsedEventHandler(t2_Elapsed);
t2.Enabled = true;
t3 = new System.Threading.Timer(new System.Threading.TimerCallback(t3_TimerCallback), null, System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
t3.Change(0, 1);
}
private void btnStop_Click( object sender, EventArgs e)
... {
t1.Enabled = false;
t2.Enabled = false;
t3.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
}
void t1_Elapsed( object sender, System.Timers.ElapsedEventArgs e)
... {
al1.Add(System.DateTime.Now.Millisecond);
}
void t2_Tick( object sender, EventArgs e)
... {
al2.Add(System.DateTime.Now.Millisecond);
}
void t3_TimerCallback( object state)
... {
al3.Add(System.DateTime.Now.Millisecond);
}
private System.Timers.Timer T2
private System.Threading.Timer T3
ArrayList al1 = new ArrayList();
ArrayList al2 = new ArrayList();
ArrayList al3 = new ArrayList();
private void btnStart_Click( object sender,EventArgs e)
... {
t1.Interval = 1;
t1.Tick += new EventHandler(t1_Tick);
t1.Enabled = true;
t2.Interval = 1;
t2.Elapsed += new System.Timers.ElapsedEventHandler(t2_Elapsed);
t2.Enabled = true;
t3 = new System.Threading.Timer(new System.Threading.TimerCallback(t3_TimerCallback), null, System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
t3.Change(0, 1);
}
private void btnStop_Click( object sender, EventArgs e)
... {
t1.Enabled = false;
t2.Enabled = false;
t3.Change(System.Threading.Timeout.Infinite, System.Threading.Timeout.Infinite);
}
void t1_Elapsed( object sender, System.Timers.ElapsedEventArgs e)
... {
al1.Add(System.DateTime.Now.Millisecond);
}
void t2_Tick( object sender, EventArgs e)
... {
al2.Add(System.DateTime.Now.Millisecond);
}
void t3_TimerCallback( object state)
... {
al3.Add(System.DateTime.Now.Millisecond);
}