Why when I do in the Console.Beep(1000, 35000); program Console.Beep(1000, 35000); then it hangs and so freezes that the message “Program does not respond” does not even appear?


UPD :

Why do I still have it freezes despite the fact that I brought beep into an asynchronous stream?

  static async Task beeping() { Console.Beep(1000, 1000); } private void refresh(object sender, EventArgs e) { label1.Content = "Последнее обновление было "+ lastref++.ToString() + " сек. назад"; if (beep == true) { var task = beeping(); task.Wait(); } } 

UPD :

Tried differently, also does not help

 static void beeping() { Console.Beep(1000, 1000); } private void refresh(object sender, EventArgs e) { label1.Content = "Последнее обновление было "+ lastref++.ToString() + " сек. назад"; if (beep == true) { SomeDelegate sd = beeping; IAsyncResult asyncRes = sd.BeginInvoke(null,null); sd.EndInvoke(asyncRes); } } 

    3 answers 3

     private void refresh(object sender, EventArgs e) { label1.Content = "Последнее обновление было "+ lastref++.ToString() + " сек. назад"; if (beep == true)Task.Factory.StartNew(() => Console.Beep(1000, 1000)); } 
    • Oh, that went so well) Thank you - alex-rudenkiy

    Because Console.Beep is a synchronous function, it blocks program execution for a certain time. If you want to prevent blocking, call it in a separate thread.

    Does the message "Program not respond" even appear?

    Because this message is relevant for programs that have a window interface and a message loop. That is, it appears when the program stops processing messages from the system. And console programs have no message loop.

    • So I say everything correctly, and if I throw it into an asynchronous stream, will it help? - alex-rudenkiy
    • @ alex-rudenkiy, run your Beep in a separate thread if you want to do something else at the same time. - aleks.andr
    • @ aleks.andr now it remains to learn how to work with asynchronous streams :) - alex-rudenkiy
    • @ aleks.andr please see why the thread does not help, updated the topic. - alex-rudenkiy
    • @ alex-rudenkiy you are forced to call .Wait (), i.e. wait for work to finish, so he hangs - etki

    See the documentation :

    Options

    frequency

     Частота сигнала в диапазоне от 37 до 32767 Гц. 

    duration

     Длительность сигнала в миллисекундах. 

    I can assume that you just can not hear your beep for 35 (!) Seconds due to the absence (or disconnection) of the built-in speaker?

    • No, everything beeps as it should, but my program doesn’t go further beep, more precisely, until it otpishit 35 seconds the program does not want to do anything. Can the squeaker output to asynchronous flow? - alex-rudenkiy
    • @ alex-rudenkiy, by itself in the same stream, where the call to Beep , the program will not do anything. Calling Beep blocks the thread of execution until it is completed. - aleks.andr