开发者

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

In C# I can perform a Console开发者_Go百科.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?


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


You can use the following code to run Console.Beep() in another thread:

System.Threading.Thread thread = new System.Threading.Thread(
    new System.Threading.ThreadStart(
        delegate()
        {
            Console.Beep();
        }
    ));

thread.Start();


You can run Console.Beep in a separate thread.


Very simple!

Task.Run(() => Console.Beep(440, 1000));

Or if you're in an async environment:

await Task.Run(() => Console.Beep(440, 1000));

https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.run?view=net-5.0#System_Threading_Tasks_Task_Run_System_Action_

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜