In C# I can perform a Console.Beep(). However, if you specify a duration of say 1000, or 1 second, it will not execute the next line of code until that second passes.
Is there any way possible to execute Console.Beep() in a non-blocking fashion so it will continue to beep and still continue executing the code below it while beeping?
The Beep(int, int) method of Console Class is used to play a Beep sound through the Console speaker at the specified frequency for a specified duration. These frequency and duration are specified as parameters to this method. By default, the beep plays at a frequency of 800 hertz for a duration of 200 milliseconds.
Given a normal Console in C#, the task is to play Beep sound through the Console. Approach: This can be achieved with the help of Beep() method of Console Class in System package of C#. The Beep() method of Console Class is used to play a Beep sound through the Console speaker. Syntax: public static void Beep ();
You can run it in a separate thread.
new Thread(() => Console.Beep()).Start();
I woke this morning to find flurry of comments on this answer. So I thought I would chime in with some other ideas.
The above can also be achieved running the thread on the Thread Pool, by using the following.
Action beep = Console.Beep;
beep.BeginInvoke((a) => { beep.EndInvoke(a); }, null);
The important thing in the above code is to call EndInvoke on your delegate if you use BeginInvoke otherwise you will experience memory leaks.
From MSDN:Important: Always call EndInvoke to complete your asynchronous call. http://msdn.microsoft.com/en-us/library/2e08f6yc(VS.80).aspx
Alternatively, you can use the dedicated Beep thread to have beeps run in the background when on demand without creating a new thread everytime or using the thread pool (see Simon Chadwick's comment). As a simple example, you could have the following. Notice that I pass 1 as the maxStackSize, this will ensure that the minimum (not 1, minimum) stack space is committed for this thread, see MSDN for more detail on this.
class BackgroundBeep
{
static Thread _beepThread;
static AutoResetEvent _signalBeep;
static BackgroundBeep()
{
_signalBeep = new AutoResetEvent(false);
_beepThread = new Thread(() =>
{
for (; ; )
{
_signalBeep.WaitOne();
Console.Beep();
}
}, 1);
_beepThread.IsBackground = true;
_beepThread.Start();
}
public static void Beep()
{
_signalBeep.Set();
}
}
With this, all you need to do to run a backround beep at anytime with out creating new threads is make the following call
BackgroundBeep.Beep();
You could use SoundPlayer.Play() and asynchronously annoy the user with something that sounds better than BEEP.
I may be missing something, but why not use:
System.Media.SystemSounds.Beep.Play();
This will play a nicer beep, asynchronously, and doesn't require the code or the overhead of the other proposed solutions.
Here's a resource friendly way to play a beep asynchronously :
Action beep = Console.Beep;
beep.BeginInvoke(null, null);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With