简体   繁体   English

发出连续的哔声,直到在C#中被打断

[英]make a continuous beep sound until interrupted in c#

I want to make a continuous beep sound in my application until interrupted. 我想在应用程序中发出连续的哔声,直到被打断为止。

Approach 方法

  1. Display a message box with retry cancel button 显示带有重试取消按钮的消息框
  2. Make beep sound continuously until I press Cancel 连续发出哔声,直到按取消
  3. If a press Retry the beep sound should not stop 如果按“重试”,哔声不会停止


I tried this using Console.beep() but it only makes sound once. 我尝试使用Console.beep()进行此操作,但只发出一次声音。
Any ideas?? 有任何想法吗??

As you know of course , this method has more constructors... you can specify it's frequency and duration : 您当然知道,此方法具有更多构造函数...您可以指定其frequencyduration

Console.Beep(100,10000);

Another suggestion for your case is just to loop it according to a boolean variable: 针对您的情况的另一建议是仅根据布尔变量进行循环:

   bool stop = false;

        while (!stop)
        {
            Console.Beep();
        }

Console.Beep(); has two arguments, length and tone. 有两个参数,长度和音调。

Use like this: Console.Beep(tone_in_hz, length_in_milliseconds); 像这样使用: Console.Beep(tone_in_hz, length_in_milliseconds);

You could also create a new thread and a public boolean and then run a while loop, like so: 您还可以创建一个新线程和一个公共布尔值,然后运行while循环,如下所示:

private void Beeper()
{
    while(makeBeepSound)
    {
        Console.Beep();
    }
}

Try this 尝试这个

   public void Beep()
    {
        if (MessageBox.Show("Message", "Alert", MessageBoxButtons.RetryCancel) == DialogResult.Retry)
        {
                Console.Beep(5000, 5000);
                Beep();
        }
    }

In Form1 load event 在Form1加载事件中

    private void Form1_Load(object sender, EventArgs e)
    {
        Beep();
    }

Place Beep() in a loop and start it in a new tread. 将Beep()循环放置,并在新的胎面中启动它。 For instance: 例如:

    class Beeper
    {
        private int _gapMiliseconds;
        private bool _stop = false;

        public Beeper(int gapMiliseconds)
        {
            _gapMiliseconds = gapMiliseconds;
        }

        public void Start()
        {
            while (!_stop)
            {
                Console.Beep();
                Thread.Sleep(_gapMiliseconds);
            }
        }

        public void Stop()
        {
            _stop = true;
        }
    }

and a code to start and stop it as a thread. 以及将其作为线程启动和停止的代码。

        Beeper beeper = new Beeper(100);
        Thread thread = new Thread(new ThreadStart(beeper.Start));
        thread.Start();
        Thread.Sleep(5000);
        beeper.Stop();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM