How to Reset Timer in C#

Muhammad Zeeshan Feb 02, 2024
  1. System.Timer Class in C#
  2. Use Timer.AutoReset to Reset Timer in C#
How to Reset Timer in C#

This article will discuss resetting a timer using the C# programming language.

System.Timer Class in C#

This System.Timer class in C# provides a mechanism to execute a block of code when a certain amount of time has passed, and this code may be performed several times.

Use Timer.AutoReset to Reset Timer in C#

When the timer’s interval has ended, AutoReset allows it to reset itself and begin counting from 0 again. The countdown will start if the enabled property is set to true, but if this is set to false, the timer will need to be restarted by making a call to the Start() method.

When the timer function of the program has been completed, the program should be terminated and its resources made available for usage in other contexts. To put it another way, the Timer.AutoReset reads from or writes to a Boolean value that indicates whether the timer should raise the Elapsed event just once false or several times true.

The syntax of the AutoReset event is below.

public bool AutoReset { get; set; }

This kind of event can be used for various things, but it is particularly well-suited for usage in web applications to determine the state of a connection. A stopwatch for use during a video game is yet another potential use.

The following example demonstrates how to construct a Timer with an Elapsed event that triggers when 2 seconds have passed. The console is then shown with the "My name is DelftStack" as the output of the associated event handler.

  1. To begin, we must import the following libraries.

    using System;
    using System.Timers;
    
  2. Declaring the timer variable, which we will refer to as t, inside the Shanitimer class ensures that it will be available to all methods.

    private static System.Timers.Timer t;
    
  3. We’ll set up a timer by following built-in classes.

    • AutoReset timer resets and begins a new count once an interval has elapsed. The timer must be reset if this is set to false.
    • 2000 is the time interval that may be altered under the circumstances. You may specify in milliseconds how long after setting the Interval parameter you want to wait before running the needed code.
    t.AutoReset = true;
    t.Enabled = true;
    t.Interval = 2000;
    t.Elapsed += ontime;
    
    • The Elapsed attribute specifies the function to be run after the time. Defining an event handler in C# may be accomplished more quickly using the +=, as I did, and it instructs the timer to subscribe to the event handler.
  4. We’ve also shown a few alert messages on the console.

    Console.WriteLine(" Waiting for Command ");
    Console.WriteLine(" Click on Enter Button to Exit Console ");
    Console.ReadLine();
    
  5. Stop the timer using the Stop() function. The Dispose() function should be used to release any resources it has consumed after the timer has finished running.

    t.Stop();
    t.Dispose();
    
  6. Object and System.Timers.ElapsedEventArgs s arguments are required for ontime() method.

    private static void ontime(Object source, System.Timers.ElapsedEventArgs s) {
      Console.WriteLine(" My name is DelftStack ");
    }
    

Full Source Code:

using System;
using System.Timers;

class Shanitimer {
  private static System.Timers.Timer t;
  static void Main() {
    t = new System.Timers.Timer();
    t.AutoReset = true;
    t.Enabled = true;
    t.Elapsed += ontime;
    t.Interval = 2000;
    Console.WriteLine(" Waiting for Command ");
    Console.WriteLine(" Click on Enter Button to Exit Console ");
    Console.ReadLine();
    t.Stop();
    t.Dispose();
  }
  private static void ontime(Object source, System.Timers.ElapsedEventArgs s) {
    Console.WriteLine(" My name is DelftStack ");
  }
}

Output:

Waiting for Command
Click on Enter Button to Exit Console
My name is DelftStack
My name is DelftStack
//After this line, I pressed enter
Muhammad Zeeshan avatar Muhammad Zeeshan avatar

I have been working as a Flutter app developer for a year now. Firebase and SQLite have been crucial in the development of my android apps. I have experience with C#, Windows Form Based C#, C, Java, PHP on WampServer, and HTML/CSS on MYSQL, and I have authored articles on their theory and issue solving. I'm a senior in an undergraduate program for a bachelor's degree in Information Technology.

LinkedIn

Related Article - Csharp Timer