简体   繁体   English

在后台播放提示音

[英]Playing Beep sound in background

With some references from other websites, I have developed a code which checks whether an Item is available for sale or not. 通过参考其他网站上的一些参考资料,我开发了一个代码来检查某项商品是否可供出售。
If the Item is unavailable, It should make a beep sound in background along with a dialog box (Retry/Cancel). 如果该项目不可用,它应该在背景声中发出哔声以及一个对话框(重试/取消)。

Further, if user clicks on Retry, The beep sound Should not stop. 此外,如果用户单击“重试”,则蜂鸣声不应停止。
Else clicking on Cancel should stop the beep sound in background. 否则,单击取消将在背景中停止发出哔声。

The code i used 我使用的代码

                    if()
                    {
                     Item exists code
                    }
                    else
                    {
                        //Item Not found
                        retry();
                    }

public void retry()
    {
        Thread beepThread = new Thread(new ThreadStart(PlayBeep));
        beepThread.IsBackground = true;

        if (MessageBox.Show("Item not found", "Alert", MessageBoxButtons.RetryCancel) == DialogResult.Retry)
        {                
            beepThread.Start();                
            retry();
        }
        else
        {
            beepThread.Abort();
            Console.Beep(500, 1);
            return;
        }
    }

    private void PlayBeep()
    {
        Console.Beep(500, int.MaxValue);
    }


Using the above code, plays the sound when I click on retry but I want it to play as soon as It enters Else condition (When Item is not found) 使用上面的代码,当我单击重试时播放声音,但是我希望它在进入其他条件时立即播放(未找到项目时)
Any suggestions? 有什么建议么?

You should start the beep sound right before the messagebox appears. 您应该在出现消息框之前立即发出蜂鸣声。 In order not to have too many unused threads you have to abort them in both cases. 为了没有太多未使用的threads ,在两种情况下都必须中止它们。
Finally I would suggest using a while(true) loop in order to get an endless beep sound. 最后,我建议使用while(true)循环以获得无尽的蜂鸣声。

    public void retry()
    {
        Thread beepThread = new Thread(new ThreadStart(PlayBeep));
        beepThread.IsBackground = true;
        beepThread.Start();

        if (MessageBox.Show("Item not found", "Alert", MessageBoxButtons.RetryCancel) == DialogResult.Retry)
        {
            beepThread.Abort();
            retry();
        }
        else
        {
            beepThread.Abort();
            Console.Beep(500, 1);
            return;
        }
    }

    private void PlayBeep()
    {
        while(true)
           { Console.Beep(500, int.MaxValue); }
    }

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

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