【问题标题】:Playing Beep sound in background在后台播放哔声
【发布时间】:2015-09-07 11:54:51
【问题描述】:

根据其他网站的一些参考资料,我开发了一个代码来检查物品是否可以出售。
如果该项目不可用,它应该在背景中发出哔声以及一个对话框(重试/取消)。

此外,如果用户单击“重试”,蜂鸣声不应停止。
否则单击“取消”应在后台停止蜂鸣声。

我使用的代码

                    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);
    }


使用上面的代码,当我点击 retry 时播放声音,但我希望它在进入 Else 条件时立即播放(当找不到项目时)
有什么建议么?

【问题讨论】:

    标签: c# audio


    【解决方案1】:

    您应该在消息框出现之前立即发出哔声。为了避免有太多未使用的threads,您必须在这两种情况下中止它们。
    最后,我建议使用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); }
        }
    

    【讨论】:

    • 谢谢@Jibbow 真的很有帮助:)
    猜你喜欢
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多