TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Console.Beep Example

This C# example program uses the Console.Beep method to make noises.

Console.Beep makes noises.

We make a C# program create a beeping sound, such as for an alarm or to annoy your coworkers while you are away. The Console.Beep method provides an exquisite beeping sound, worthy of music industry awards.

Example. First, we use a C# console application for this to work. The Console.Beep method has two overloads. The first version is the default beep. The second version receives two arguments—the frequency and the duration in milliseconds.

Overload

Here: We loop through all the frequencies at an increment of 200. This produces a musical effect over a few seconds when executed.

C# program that uses Console.Beep method

using System;

class Program
{
    static void Main()
    {
	// The official music of Dot Net Perls.
	for (int i = 37; i <= 32767; i += 200)
	{
	    Console.Beep(i, 100);
	}
    }
}

Beep can be used for simple alarm programs. For example, you might want to create a program that executes for 30 minutes and then starts beeping. You could create primitive alarm clocks and use your computer to wake up in the morning.

Note: The Console.Beep method is not supported on some versions of 64-bit Windows. Thanks to Max for writing in with this information.

Alarm. This is an alarm program. It sets off an audible alarm after a certain number of minutes. The program simply asks for the number of minutes. It converts this to a string with the int.Parse method.

Then: It waits sixty seconds using Thread.Sleep for each minute, and writes some characters to the screen. Finally it beeps ten times.

ParseThread.Sleep

Example alarm program: C#

using System;
using System.Threading;

class Program
{
    static void Main()
    {
	// Ask for minutes.
	Console.ForegroundColor = ConsoleColor.White;
	Console.WriteLine("Minutes?");
	string minutes = Console.ReadLine();
	int mins = int.Parse(minutes);
	for (int i = 0; i < mins; i++)
	{
	    // Sixty seconds is one minute.
	    Thread.Sleep(1000 * 60);
	    // Write line.
	    Console.WriteLine(new string('X', 30));
	}
	// Beep ten times.
	for (int i = 0; i < 10; i++)
	{
	    Console.Beep();
	}
	Console.WriteLine("[Done]");
	Console.Read();
    }
}

This program is simple. The simplicity makes it easy to maintain and understand but not fancy. For common tasks that require a timer, this program can be useful. An elaborate solution can be found using Windows Forms and sound effects.

Windows Forms

Summary. The amazing beeping effect was presented. Instead of downloading MP3s on your computer, you can simply use a random number generator and the Console.Beep method to generate your own music.

Random

Also: A strong painkiller, or some other form of assistance, will be required after a few minutes.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf