Timer.Elapsed fire event only once,I want it every Second

问题描述:

Timers.Timer创建StopWatch。我使用Timer.Elapsed来处理在特定时间后显示时间的事件。我给定时器间隔为1,并启用为true。我也使AutoReset为true。但问题是,事件只发射once.and我得到的时间在文本框,我只once.How可以改变时间的TextBox每Second.I想尽一切办法,但没有获得成功......谢谢你Timer.Elapsed fire event only once,I want it every Second

System.Timers.Timer StopWatchTimer = new System.Timers.Timer(); 
    Stopwatch sw = new Stopwatch(); 
    public void StopwatchStartBtn_Click(object sender, ImageClickEventArgs e) 
    { 

     StopWatchTimer.Start(); 
     StopWatchTimer.Interval = 1; 
     StopWatchTimer.Enabled = true; 
     StopWatchTimer.AutoReset =true; 
     sw.Start(); 
     StopWatchTimer.Elapsed += new System.Timers.ElapsedEventHandler(StopWatchTimer_Tick); 
    } 

    protected void StopWatchStopBtn_Click(object sender, ImageClickEventArgs e) 
    { 

     TextBoxStopWatch.Text = "00:00:000"; 
     StopWatchTimer.Stop(); 
     sw.Reset(); 
     sw.Stop(); 

    } 

    public void StopWatchTimer_Tick(object sender,EventArgs e) 
    {   
    TextBoxStopWatch.Text= Convert.ToString(sw.Elapsed); 
    } 

更新: 我尝试通过在Visual Studio中创建新网站。但仍然没有得到success.same问题。现在更新是,当我在线路设置断点

 TextBoxStopWatch.Text= Convert.ToString(sw.Elapsed); 

文本是不断变化的有,但在文本框不显示。希望你能理解这一点。

+0

当我运行你的代码时,我得到一个InvalidOperationException。原因是StopWatchTimer_Tick方法试图访问不同线程中的控件。代码的其余部分是什么? – NotMe 2012-01-09 18:17:15

+0

有趣的是,如果我直接编译并运行(在VS之外),它可以按预期工作。你使用的是什么版本的C#? – NotMe 2012-01-09 18:24:02

+0

@Chris Lively .NET Framework 4.0 – Hiren 2012-01-09 18:34:07

你不能这样做在网页上。

在页面呈现时,服务器完成并且客户端已断开连接。它不会从你的计时器获得更多的更新。

如果你需要在页面本身显示一些变化的数字,那么你将不得不通过JavaScript做到这一点。

您甚至在设置参数之前打电话给Start()。试试这个:

StopWatchTimer.Interval = 1000; 
StopWatchTimer.AutoReset = true; 
StopWatchTimer.Elapsed += new System.Timers.ElapsedEventHandler(StopWatchTimer_Tick); 
StopWatchTimer.Enabled = true; 

设置Enabled属性true所有属性都设置后。 (调用Start()方法相当于设置Enabled = true

此外,不知道您是否知道这一点,但Timer.Interval属性以毫秒为单位。所以你每毫秒触发一次Timer.Elapsed事件。仅供参考。

+0

谢谢你的回答。我应该在开始之前设置属性。我这样做,但我仍然只在文本框中获得价值一次,而不会引发事件 – Hiren 2012-01-09 18:05:29

+0

尝试将间隔设置为1000.并且,如何知道计时器。经历的事件只发射一次? – 2012-01-09 18:11:41

+0

其实我在那里设置BreakPoint.After设置为1000我甚至不会得到它一次 – Hiren 2012-01-09 18:12:40

您还需要考虑文本框的内容应该只能由UI线程而不是回调上下文更改。你在回调中遇到异常吗? 查看使用调度程序调用主UI线程而不是定时器线程的UI更新。

以下适用于我。我重新安排了事情,所以定时器/秒表的设置只设置一次,以避免混淆,并处理UI线程调用。

System.Timers.Timer timer; 
    Stopwatch stopwatch; 

    public Form1() 
    { 
     InitializeComponent(); 

     timer = new System.Timers.Timer(); 
     timer.Interval = 1000; 
     timer.AutoReset = true; 
     timer.Elapsed += new ElapsedEventHandler(TimerElapsed); 

     stopwatch = new Stopwatch(); 
    } 

    public void TimerElapsed(object sender, EventArgs e) 
    { 
     TextBoxStopWatch.Text = Convert.ToString(stopwatch.Elapsed); 
    } 

    private void btnStart_Click(object sender, EventArgs e) 
    { 
     timer.Start(); 
     stopwatch.Start(); 
    } 

    private void btnStop_Click(object sender, EventArgs e) 
    { 
     TextBoxStopWatch.Text = "00:00:000"; 
     timer.Stop(); 
     stopwatch.Reset(); 
     stopwatch.Stop(); 
    } 
+0

感谢您的回复我正在使用Web应用程序而不是Windows应用程序,所以我没有调用或InvokeRequired。 – Hiren 2012-01-09 19:14:57