Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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!


C#
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);
            myDispatchTimer.Start();
        }
        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.Stop();
            myDispatchTimer.Tick -= new EventHandler(Initial_Wait);
            myDispatchTimer.Interval = TimeSpan.FromSeconds(5); //every x seconds
            myDispatchTimer.Tick += new EventHandler(Each_Tick);
            myDispatchTimer.Start();
        }

        // Counter:
        int i = 0;
        // Ticker
        void Each_Tick(object o, EventArgs sender)
        {
            GetMessageDeliveryStatus(messageID, messageKey);
            textBlock1.Text = "Seconds: " + i++.ToString();
        }
Posted

1 solution

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:
C#
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);
    timer.Start();
}

void timer_Tick(object sender, EventArgs e)
{
    if (firstRun)
    {
        timer.Interval = TimeSpan.FromSeconds(5);
        firstRun = false;
    }
    if (DateTime.Now.Subtract(startTime).TotalSeconds > 60)
    {
        timer.Stop();
    }
    else
    {
        var t = new System.Threading.Thread(new System.Threading.ThreadStart(delegate()
        {
            // ...do stuff that takes a while....
        }));
        t.Start();
        // ...or do stuff that runs quickly....
    }
}
 
Share this answer
 
v3
Comments
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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900