none
How to stop time after 30 seconds RRS feed

  • Question

  • publicclassExample     {         privatestaticTimer aTimer;         publicstaticvoid Main()         {             // Create a timer and set a two second interval.             aTimer = new System.Timers.Timer();             aTimer.Interval = 1000;             // Hook up the Elapsed event for the timer.              aTimer.Elapsed += OnTimedEvent;             // Have the timer fire repeated events (true is the default)             aTimer.AutoReset = true;             // Start the timer             aTimer.Enabled = true;             Console.WriteLine("Press the Enter key to exit the program at any time... ");             Console.ReadLine();         }         privatestaticvoid OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)         {             // Timer is running Continously. //I want to stop the timer after 30 secs.Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);         }     }

    here I want to stop my timer after 30 secs.

    Any peice of code is much appericaited.

                                      
    Monday, July 9, 2018 12:49 PM

Answers

  • Hi. How about creating an advance timer which extend the OOTB timer one like this:

    public class SmartTimer: System.Timers.Timer
        {
            private DateTime startTime;
            private uint ticks;
                
            /// <summary>
            ///     The maximum ticks allowed for the timer to be triggered!
            /// </summary>
            public uint MaximumTicks { get; set; }
    
            /// <summary>
            ///     The time span allowed for the timer to run.
            /// </summary>
            public TimeSpan MaximumDuration { get; set; }
    
            public SmartTimer()
            {
                this.Elapsed += MyTimer_Elapsed;
            }
    
            private void MyTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                var myTimer = sender as System.Timers.Timer;
    
                if (this.MaximumDuration != new TimeSpan())
                {
                    if (this.startTime == new DateTime())
                    {
                        this.startTime = e.SignalTime;
                    }
    
                    var timePassed = e.SignalTime - this.startTime;
                    if (timePassed >= this.MaximumDuration)
                    {
                        myTimer.Stop();
                    }
                }
    
                if (this.MaximumTicks != 0)
                {
                    this.ticks++;
                    if (this.ticks == this.MaximumTicks)
                    {
                        myTimer.Stop();
                    }
                }
            }
        }

    In this class, you would have two new properties and you can use either of them or both of them if you want to stop the timer with either two different conditions:

    1. MaximumDuration
      This is would be the allowed time span for the timer to keep running. E.g. for your case it should be set to new TimeSpan(0, 0, 30);
                  timer.MaximumTicks = 10;
    2. MaximumTicks
      This would be the maximum ticks allowed for the timer to be triggered. E.g. for your case it should be set to 30 as your interval is 1000 milliseconds which is one second!
    class Program
        {
            static void Main()
            {
                //var timer = new System.Timers.Timer();
                var timer = new SmartTimer();
                timer.MaximumDuration = new TimeSpan(0, 0, 30);
                // ---------------- Or ------------------------
                // timer.MaximumTicks = 30;
                // --------------------------------------------
                timer.Interval = 1000;
                timer.Elapsed += Timer_Elapsed;
                timer.AutoReset = true;
                timer.Enabled = true;
                Console.WriteLine("Hit any key ...");
                Console.ReadLine();
    
                var sm = new SmartTimer();
            }
    
            private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                Console.WriteLine(e.SignalTime);
            }
        }

    • Marked as answer by Its me Gopi Tuesday, July 10, 2018 4:55 AM
    Monday, July 9, 2018 4:18 PM

All replies

  • Hi Gopi,

    add a static count to stop the timer.

    private static int count = 0;

            private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
            {
                // Timer is running Continously. //I want to stop the timer after 30 secs.Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
    
                if (count == 30)
                {
                    aTimer.Enabled = false;
                    Console.WriteLine("count stop");
                }
                else
                {
                    Console.WriteLine("count = {0}", count);
                }
                count++;
            }

    Thanks and Best regards,

    E-John


    • Edited by E-John Monday, July 9, 2018 1:07 PM
    Monday, July 9, 2018 1:06 PM
  • Hi. How about creating an advance timer which extend the OOTB timer one like this:

    public class SmartTimer: System.Timers.Timer
        {
            private DateTime startTime;
            private uint ticks;
                
            /// <summary>
            ///     The maximum ticks allowed for the timer to be triggered!
            /// </summary>
            public uint MaximumTicks { get; set; }
    
            /// <summary>
            ///     The time span allowed for the timer to run.
            /// </summary>
            public TimeSpan MaximumDuration { get; set; }
    
            public SmartTimer()
            {
                this.Elapsed += MyTimer_Elapsed;
            }
    
            private void MyTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                var myTimer = sender as System.Timers.Timer;
    
                if (this.MaximumDuration != new TimeSpan())
                {
                    if (this.startTime == new DateTime())
                    {
                        this.startTime = e.SignalTime;
                    }
    
                    var timePassed = e.SignalTime - this.startTime;
                    if (timePassed >= this.MaximumDuration)
                    {
                        myTimer.Stop();
                    }
                }
    
                if (this.MaximumTicks != 0)
                {
                    this.ticks++;
                    if (this.ticks == this.MaximumTicks)
                    {
                        myTimer.Stop();
                    }
                }
            }
        }

    In this class, you would have two new properties and you can use either of them or both of them if you want to stop the timer with either two different conditions:

    1. MaximumDuration
      This is would be the allowed time span for the timer to keep running. E.g. for your case it should be set to new TimeSpan(0, 0, 30);
                  timer.MaximumTicks = 10;
    2. MaximumTicks
      This would be the maximum ticks allowed for the timer to be triggered. E.g. for your case it should be set to 30 as your interval is 1000 milliseconds which is one second!
    class Program
        {
            static void Main()
            {
                //var timer = new System.Timers.Timer();
                var timer = new SmartTimer();
                timer.MaximumDuration = new TimeSpan(0, 0, 30);
                // ---------------- Or ------------------------
                // timer.MaximumTicks = 30;
                // --------------------------------------------
                timer.Interval = 1000;
                timer.Elapsed += Timer_Elapsed;
                timer.AutoReset = true;
                timer.Enabled = true;
                Console.WriteLine("Hit any key ...");
                Console.ReadLine();
    
                var sm = new SmartTimer();
            }
    
            private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                Console.WriteLine(e.SignalTime);
            }
        }

    • Marked as answer by Its me Gopi Tuesday, July 10, 2018 4:55 AM
    Monday, July 9, 2018 4:18 PM