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

In C #, I can execute Console.Beep (). However, if you specify a duration of 1000 or 1 second, it will not execute the next line of code until that second has passed.

Is it possible to somehow execute Console.Beep () in non-blocking mode so that it continues to sound and continues to execute the code under it during a beep?

+13
source share
6 answers

You can run it in a separate thread.

new Thread(() => Console.Beep()).Start();

I woke up this morning to find a flurry of comments on the subject. So I thought that I would have something in common with some other ideas.

, .

Action beep = Console.Beep;
beep.BeginInvoke((a) => { beep.EndInvoke(a); }, null);

, EndInvoke , BeginInvoke, .

MSDN: . EndInvoke . http://msdn.microsoft.com/en-us/library/2e08f6yc(VS.80).aspx

Beep (. ). . , 1 maxStackSize, , ( 1, ) , . MSDN .

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

, , ,

BackgroundBeep.Beep();
+19
+7

, - , :

System.Media.SystemSounds.Beep.Play();

, , .

+6

:

Action beep = Console.Beep;
beep.BeginInvoke(null, null); 
+4

Console.Beep() :

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

thread.Start();
+3

Console.Beep .

+1

Source: https://habr.com/ru/post/1758069/


All Articles