End Google Ads 201810 - BS.net 01 --> Hi,

I am trying to write a small application, using Visual C++.

In the application's GUI, among other things, there will be 2 buttons:
First button should turn the PC's internal speaker ON.
Second button should turn the PC's internal speaker OFF.

The GUI will have to be functional during all times.
I see that in order to use the PC's internal speaker I need to use the Beep(int tone, int duration) function. (or is there any other way ???)

The problem is that Beep function will play sound and stop the code's execution until duration mSec will be over.
This is not good to me, as I will need the program to continue do other things in the background and I will need it to response to the OFF button as well.

Using a flag to repeat the short Beeps again and again in a loop produces a glitch in the sound, every time that the Beep is executed (looped) again, so this is not elegant solution that I wold like to use.

The solution that I came to is:
ON button will start a thread, that will Beep endlessly.
OFF button will Beep for 1 mSec, and this way will terminate sound that the Beep from the BeepThread started.

Note that the Beep's thread called from ON button will never finish execution as it will be stack in it's Beep() line that has endless time as parameter.

The problem in my solution is that even if the sound stops, the BeepThread that was started will never end, and pressing several times on the on/off buttons will produce several BeepThreads.

Killing a thread from the outside is not recommended as well.

Any ideas on how to deal with this issue ?
I am open to change the whole way of my solution.

Thanks.