悬赏园豆:50
[已关闭问题]
<P>声明: <BR>System.Timers.Timer timer = new System.Timers.Timer(); <BR>timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); <BR><BR>button按钮事件: <BR>timer.AutoReset = true; <BR>timer.Interval =15; <BR>timer.Start (); <BR><BR>timer调用的函数: <BR>void timer_Elapsed(object sender, ElapsedEventArgs e) <BR>{ <BR> Console.Out.WriteLine(DateTime.Now +" "+ DateTime.Now.Millisecond.ToString() + "timer in:" + nCountTimer.ToString()); <BR>} <BR><BR>运行结果: <BR>运行程序后第一次点击button事件: <BR>2008-5-8 17:38:21 170timer in:0 <BR>2008-5-8 17:38:21 170timer in:1 <BR>2008-5-8 17:38:21 190timer in:2 <BR>2008-5-8 17:38:21 210timer in:3 <BR>不解?为什么前2次进入的时间相同?(每次尝试都是这样) <BR><BR>第二次点击button事件: <BR>2008-5-8 17:38:23 383timer in:0 <BR>2008-5-8 17:38:23 403timer in:1 <BR>2008-5-8 17:38:23 423timer in:2 <BR>2008-5-8 17:38:23 443timer in:3 <BR>2008-5-8 17:38:23 463timer in:4 <BR>运行正常,20ms进入一次,但我设置的进入间隔为15ms!</P>
<P> </P>
<P>再请教一下 如何让timers.timer单线程执行?lock怎么用?我加上了还是会同时多次进入!</P>
问题补充:
附加独立测试代码
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Timers;
namespace WindowsApplication3
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
System.Timers.Timer timer = new System.Timers.Timer();
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.AutoReset = true;
timer.Interval =60;
timer.Start();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
public static void timer_Elapsed(object sender, ElapsedEventArgs e)
{
Console.Out.WriteLine(DateTime.Now + " " + DateTime.Now.Millisecond + " "+DateTime .Now.TimeOfDay.TotalMilliseconds );
}
}
}
运行结果: 间隔都设置到60了 初次进入达到了3次
2008-5-9 10:08:38 155 36518155.912
2008-5-9 10:08:38 155 36518155.912
2008-5-9 10:08:38 155 36518155.912
2008-5-9 10:08:38 185 36518185.9552
2008-5-9 10:08:38 246 36518246.0416
2008-5-9 10:08:38 306 36518306.128
2008-5-9 10:08:38 366 36518366.2144
2008-5-9 10:08:38 426 36518426.3008
2008-5-9 10:08:38 486 36518486.3872
2008-5-9 10:08:38 546 36518546.4736
2008-5-9 10:08:38 606 36518606.56
2008-5-9 10:08:38 666 36518666.6464