Console::Beep Method (Int32, Int32)
Plays the sound of a beep of a specified frequency and duration through the console speaker.
Assembly: mscorlib (in mscorlib.dll)
public: [HostProtectionAttribute(SecurityAction::LinkDemand, UI = true)] static void Beep( int frequency, int duration )
Parameters
- frequency
-
Type:
System::Int32
The frequency of the beep, ranging from 37 to 32767 hertz.
- duration
-
Type:
System::Int32
The duration of the beep measured in milliseconds.
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | frequency is less than 37 or more than 32767 hertz. -or- duration is less than or equal to zero. |
| HostProtectionException | This method was executed on a server, such as SQL Server, that does not permit access to the console. |
Beep wraps a call to the Windows Beep function. Whether Beep produces a sound on versions of Windows before Windows 7 depends on the presence of a 8254 programmable interval timer chip. Starting with Windows 7, it depends on the default sound device.
Note |
|---|
The Beep method is not supported on the 64-bit editions of Windows Vista and Windows XP. |
The frequency and duration parameters are ignored for these operating systems.
This example demonstrates the Beep method by playing the first few notes of a song through the console speaker.
// This example demonstrates the Console.Beep(Int32, Int32) method using namespace System; using namespace System::Threading; ref class Sample { protected: // Define the frequencies of notes in an octave, as well as // silence (rest). enum class Tone { REST = 0, GbelowC = 196, A = 220, Asharp = 233, B = 247, C = 262, Csharp = 277, D = 294, Dsharp = 311, E = 330, F = 349, Fsharp = 370, G = 392, Gsharp = 415 }; // Define the duration of a note in units of milliseconds. enum class Duration { WHOLE = 1600, HALF = Duration::WHOLE / 2, QUARTER = Duration::HALF / 2, EIGHTH = Duration::QUARTER / 2, SIXTEENTH = Duration::EIGHTH / 2 }; public: // Define a note as a frequency (tone) and the amount of // time (duration) the note plays. value struct Note { public: Tone toneVal; Duration durVal; // Define a constructor to create a specific note. Note( Tone frequency, Duration time ) { toneVal = frequency; durVal = time; } property Tone NoteTone { // Define properties to return the note's tone and duration. Tone get() { return toneVal; } } property Duration NoteDuration { Duration get() { return durVal; } } }; protected: // Play the notes in a song. static void Play( array<Note>^ tune ) { System::Collections::IEnumerator^ myEnum = tune->GetEnumerator(); while ( myEnum->MoveNext() ) { Note n = *safe_cast<Note ^>(myEnum->Current); if ( n.NoteTone == Tone::REST ) Thread::Sleep( (int)n.NoteDuration ); else Console::Beep( (int)n.NoteTone, (int)n.NoteDuration ); } } public: static void Main() { // Declare the first few notes of the song, "Mary Had A Little Lamb". array<Note>^ Mary = {Note( Tone::B, Duration::QUARTER ),Note( Tone::A, Duration::QUARTER ),Note( Tone::GbelowC, Duration::QUARTER ),Note( Tone::A, Duration::QUARTER ),Note( Tone::B, Duration::QUARTER ),Note( Tone::B, Duration::QUARTER ),Note( Tone::B, Duration::HALF ),Note( Tone::A, Duration::QUARTER ),Note( Tone::A, Duration::QUARTER ),Note( Tone::A, Duration::HALF ),Note( Tone::B, Duration::QUARTER ),Note( Tone::D, Duration::QUARTER ),Note( Tone::D, Duration::HALF )}; // Play the song Play( Mary ); } }; int main() { Sample::Main(); } /* This example produces the following results: This example plays the first few notes of "Mary Had A Little Lamb" through the console speaker. */
Available since 2.0
