Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I execute a non-blocking System.Beep()?

Tags:

c#

.net

console

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?

like image 457
Siracuse Avatar asked May 01 '10 21:05

Siracuse


People also ask

What does console beep do in C#?

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.

How do you make a beeping sound in C#?

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 ();


4 Answers

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();
like image 110
Chris Taylor Avatar answered Oct 07 '22 08:10

Chris Taylor


You could use SoundPlayer.Play() and asynchronously annoy the user with something that sounds better than BEEP.

like image 27
Hans Passant Avatar answered Oct 07 '22 08:10

Hans Passant


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.

like image 7
Cameron Peters Avatar answered Oct 07 '22 07:10

Cameron Peters


Here's a resource friendly way to play a beep asynchronously :

Action beep = Console.Beep;
beep.BeginInvoke(null, null); 
like image 4
Pop Catalin Avatar answered Oct 07 '22 07:10

Pop Catalin