How To Make The Console Beep in C# With A Timer

Aryan KAryan K
4 min read

C# is an object-oriented programming language developed by Microsoft. It is very similar to Java and is a part of the .NET framework which consists of various languages like F#, VB .NET etc.

C# has some cool features, one of them being the ability to make the console beep. Today, we'll be learning how to implement a beeper that beeps every 2 minutes.

Getting Started

To get started, install Visual Studio 2022 and install the C# development pack.

Once you're done with that, open Visual Studio and click on create new project and then select C# console app.

Then, you can give it any name. I'm calling it beeper.

Then choose the default .NET 6.0 Framework and click create.

Once you're done with that, a new window will open and you'll see some sample code, now we're ready to start coding in C#.

Coding Part

Let's delete the sample code that was given to us by Visual Studio and replace it with a few include statements. We'll be using System and System.Timer for this program. These are built in libraries.

//including libraries
using System;
using System.Timer;

Now let's create a class and a main function to write our program.

class beepio
{
    public static void Main(string[] args)
    {

    }
}

Now let's create a timer object that accepts the time as an argument in milliseconds. In this program, we'll be setting the time to 2 minutes so its 120000 milliseconds.

public static void Main(string[] args)
    {
        // Create a timer with a two-minute interval (120,000 milliseconds).
        System.Timers.Timer timer = new System.Timers.Timer(120000);
    }

Now let's hook up an elapsed event for the timer

public static void Main(string[] args)
    {

        System.Timers.Timer timer = new System.Timers.Timer(120000);
        // Hook up the Elapsed event for the timer.
        timer.Elapsed += OnTimedEvent;
    }

Now let's create an event to define OnTimedEvent to define what's gonna happen once the timer reaches 2 minutes.

private static void OnTimedEvent(object sender, ElapsedEventArgs e)
    {
        // Perform the desired action (in this case, beep) every 2 minutes.
        Console.Beep(1060,1060);
    }

Here, we're going to use the Console.Beep() feature in C#. It is a built-in feature. It can be used with or without arguments. It accepts 2 arguments, frequency and duration. I've given it the values of 1060 and 1060. Anything less than 37 as frequency will throw an exception and the code won't work. Don't set the value too low or else you won't be able to hear the beep.

Now back to the main function, we'll enable the timer and set the auto reset to true.

public static void Main(string[] args)
    {

        System.Timers.Timer timer = new System.Timers.Timer(120000);
        timer.Elapsed += OnTimedEvent;
        //setting auto reset to true
        timer.AutoReset = true;
        //enabling the timer
        timer.Enabled = true;
    }

Now let's create a Console.WriteLine() and ReadLine() statement to accept input from user if they wish to stop the beeping. Let's start the timer while we are at it too.

public static void Main(string[] args)
    {

        System.Timers.Timer timer = new System.Timers.Timer(120000);
        timer.Elapsed += OnTimedEvent;
        timer.AutoReset = true;
        timer.Enabled = true;
        //writeline to tell the user that we want them to press enter
        Console.WriteLine("Press Enter to stop the beep timer.");

        // Start the timer.
        timer.Start();

        // Wait for the user to press Enter to stop the timer.
        Console.ReadLine();
    }

Finally, Let's stop the timer in case the user chooses to stop the timer and then dispose of the timer.

public static void Main(string[] args)
    {

        System.Timers.Timer timer = new System.Timers.Timer(120000);
        timer.Elapsed += OnTimedEvent;
        timer.AutoReset = true;
        timer.Enabled = true;
        Console.WriteLine("Press Enter to stop the beep timer.");
        timer.Start();
        Console.ReadLine();

        // Stop and dispose of the timer when the user presses Enter.
        timer.Stop();
        timer.Dispose();
    }

Now we're done with the coding part.

Final Code

This is how the final code will look like.


using System;
using System.Timers;

class beepio
{
    public static void Main(string[] args)
    {

            System.Timers.Timer timer = new System.Timers.Timer(120000);
            timer.Elapsed += OnTimedEvent;
            timer.AutoReset = true;
            timer.Enabled = true;
            Console.WriteLine("Press Enter to stop the beep timer.");
            timer.Start();
            Console.ReadLine();


            timer.Stop();
            timer.Dispose();

    }
    private static void OnTimedEvent(object sender, ElapsedEventArgs e)
    {

        Console.Beep(1060, 1060);
    }
}

Now you can run the code and you'll hear the beep once every 2 minutes.

Modified and A Better Version Of This Program

I've also made another program out of this and it's a menu driven program which makes it easier for the user to use the program, here is the Github Repository for it.

https://github.com/SpaciousCoder78/beepio

0
Subscribe to my newsletter

Read articles from Aryan K directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aryan K
Aryan K

I'm a blogger, tech enthusiast and a novice programmer