Home
Search
Console.Beep ExampleUse the Console.Beep method to alert users. Beeps can be used for alarms.
C#
This page was last reviewed on Nov 17, 2022.
Console.Beep makes noises. We make a C# program create a beeping sound, such as for an alarm or to annoy other people. The Console.Beep method provides an exquisite beeping sound.
Random beeping. For maximum annoyance, consider using a random number generator like the Random class. And then generate random beeps.
Random
Example code. The Console.Beep method has 2 overloads. The first version is the default beep. The second version receives 2 arguments—the frequency and the duration in milliseconds.
Here We loop through all the frequencies at an increment of 200. This produces a musical effect over a few seconds when executed.
Info Beep can be used for alarms. For example, you might want to create a program that executes for 30 minutes and then starts beeping.
Note The Console.Beep method is not supported on some versions of 64-bit Windows.
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); } } }
Alarm. This is an alarm program. It sets off an audible alarm after a certain number of minutes. The program asks for the number of minutes.
Then It waits sixty seconds using Thread.Sleep for each minute, and writes some characters to the screen. Finally it beeps ten times.
int.Parse
Sleep
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]"); } }
Summary. The beeping effect was presented. Instead of downloading music, you can use a random number generator and the Console.Beep method to generate your own music.
Console
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 17, 2022 (simplify).
Home
© 2007-2022 sam allen.
see site info on the changelog.