I have a device that a user interacts with alongside of a C# WPF program. This program must beep when the user presses a button on the device, for either a specified amount of time or for as long as the user presses the button, whichever is shorter. The only speaker available to produce the beep/tone is the computer BIOS speaker; we cannot assume that other speakers are around (and it's actually safe to assume that there won't be any other speakers).
How can I produce a continuous tone for the necessary duration?
What I have so far produces lots of beeps, but not a continuous tone.
First, a thread is started:
private void UserControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e) {
if(this.Visibility == Visibility.Visible){
mBeepThread = new Thread(new ThreadStart(ProduceTone));
mBeepThread.Name = "Beep Thread";
mBeepThread.Start();
}
}
The thread itself:
bool mMakeTone = false;
private void ProduceTone(){
while(this.Visibility == Visibility.Visible){
if(mMakeTone ){
Console.Beep();
}
else{
Thread.Sleep(10);
}
}
}
And then the mMakeTone boolean is flipped to true during the duration of the button press, up to a time specified by the device itself.
I suspect that it's just a quick change to the Console.Beep() line above, but I'm not sure what it would be.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…