Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Silverlight
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);
            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-Feb-13 12:38pm
VSS712J468

1 solution

Rate this: bad
good
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);
    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....
    }
}
  Permalink  
v3
Comments
VSS712J at 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 at 4-Feb-13 12:15pm
   
I updated my answer with a code sample.
VSS712J at 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 at 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 at 13-Feb-13 17:40pm
   
Answer updated.
VSS712J at 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
0 Sergey Alexandrovich Kryukov 530
1 mhegazy94 460
2 Ravi Bhavnani 230
3 Kornfeld Eliyahu Peter 185
4 Shemeemsha RA 160
0 Sergey Alexandrovich Kryukov 7,205
1 OriginalGriff 6,801
2 CPallini 5,350
3 George Jonsson 3,644
4 Gihan Liyanage 2,797


Advertise | Privacy | Mobile
Web03 | 2.8.140922.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2014
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