Click here to Skip to main content
13,513,137 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a Silverlight Desktop Application which starts a timer after a button is clicked. The Timer Waits for 10 seconds, then, it will reset the timer to loop every 5 seconds.

How do I stop the timer after 60 seconds have elapsed? Someone recommended the StopWatch, but it's not available in Silverlight Desktop.

Here is my code that works, but it does not stop after 60 seconds:

Thank you!

public void StartTimer()
            System.Windows.Threading.DispatcherTimer myDispatchTimer = new System.Windows.Threading.DispatcherTimer();
            myDispatchTimer.Interval = TimeSpan.FromSeconds(10); // initial 10 second wait
            myDispatchTimer.Tick += new EventHandler(Initial_Wait);
        void Initial_Wait(object o, EventArgs sender)
            System.Windows.Threading.DispatcherTimer myDispatchTimer = new System.Windows.Threading.DispatcherTimer();
            // Stop the timer, replace the tick handler, and restart with new interval.
            myDispatchTimer.Tick -= new EventHandler(Initial_Wait);
            myDispatchTimer.Interval = TimeSpan.FromSeconds(5); //every x seconds
            myDispatchTimer.Tick += new EventHandler(Each_Tick);

        // Counter:
        int i = 0;
        // Ticker
        void Each_Tick(object o, EventArgs sender)
            GetMessageDeliveryStatus(messageID, messageKey);
            textBlock1.Text = "Seconds: " + i++.ToString();
Posted 1-Feb-13 12:38pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Create a member variable, startTime, and set it to DateTime.Now in StartTimer. Then, in Initial_Wait, subtract startTime from DateTime.Now and check the TotalSeconds. If greater than 60 seconds, stop the timer. Alternatively, you could use a counter approach (as you appear to be partially doing with your member variable "i").

Also, based on the sample code you provided, you will not have one timer, but many which will execute alongside each other. Move myDispatchTimer out of the functions and put only one at the class level (this is called a member variable). Only create a new instance of it once. Calling Stop() on a new instance will not stop the previous instance from running.

EDIT: Here's an example of some code you could use:
private System.Windows.Threading.DispatchTimer timer = new System.Windows.Threading.DispatcherTimer();
private bool firstRun = true;
private DateTime startTime = DateTime.MinValue;

private void StartTimer()
    startTime = DateTime.Now;
    timer.Interval = TimeSpan.FromSeconds(10);
    timer.Tick += new EventHandler(timer_Tick);

void timer_Tick(object sender, EventArgs e)
    if (firstRun)
        timer.Interval = TimeSpan.FromSeconds(5);
        firstRun = false;
    if (DateTime.Now.Subtract(startTime).TotalSeconds > 60)
        var t = new System.Threading.Thread(new System.Threading.ThreadStart(delegate()
            // stuff that takes a while....
        // ...or do stuff that runs quickly....
VSS712J 4-Feb-13 11:52am
I'm new to C# not certain how to stop the timer. Could you please elaborate with code? Thank you
AspDotNetDev 4-Feb-13 12:15pm
I updated my answer with a code sample.
VSS712J 13-Feb-13 16:20pm
Thank you very much, AspDotNetDev
Can you tell me where and how I stop the timer after 60 seconds ? would it be on the if(firstRun) line?
VSS712J 13-Feb-13 16:35pm
One more issue it seems as though the UI freezes for 10 seconds. Is there anyway all other background processing can continue without freezing ?
Thank you again
AspDotNetDev 13-Feb-13 17:40pm
Answer updated.
VSS712J 13-Feb-13 18:26pm
Awesome. So many thanks !!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.180417.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100