简体   繁体   中英

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

Timers.Timer to create StopWatch. I use Timer.Elapsed to handle event that display time after perticular time. I give timer interval of 1 and enabled to true. I also make AutoReset to true. But the problem is event is firing only once.and I get time in Textbox only once.How can I change Time in TextBox Every Second.I try all alternatives but not get success...Thank You

    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);
    }

UPDATE: I try it by creating new website in Visual Studio.But still don't get success.same problem . Now update is when I set Break Point in Line

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

Text is Continuously changing there but not displaying in TextBox. Hope You can Understand this.

You're calling Start() before you even set the parameters. Try this:

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

Set the Enabled property to true after all of your properties are set. (calling the Start() method is equivalent to setting Enabled = true )

Also, not sure if you're aware of this, but the Timer.Interval property is in milliseconds. So you are firing the Timer.Elapsed event every millisecond. Just an FYI.

You can't do it this way in a web page.

By the time the page is rendered, the server is done and the client has disconnected. It's not going to get any more updates from your timer.

If you need to have a timer on the page itself showing some changing numbers then you will have to do this via javascript.

You also need to take into account that the text box's content should only be altered by the UI thread and not the callback context. Are you taking an exception on the callback? Look at using the dispatcher to invoke the UI update on the main UI thread instead of the timer thread.

The following works for me. I rearranged things so the settings for the timer/stopwatch are only set once to avoid confusion and also dealt with the UI thread invoke.

    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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM