开发者

Play sound till dialogbox.dialogresult == dialogresult.OK

I am developing a winform application. I want to play a sound file, till the dialogresult is OK for a message box. How can i achieve that.

For e.g.

开发者_如何学编程
 /*Till*/ (MessageBox.Show("Alarm") == DialogResult.OK)

 //Play a sound file

I tried while and do while but didn't succeed. Do i need to use background worker or run the code in a separate thread ??


You don't need to roll your own threading. .NET class library already did it for you. This is how you can do it:

        SoundPlayer p = new SoundPlayer(@"C:\Windows\Media\chimes.wav");
        p.PlayLooping();
        MessageBox.Show("Alarm");
        p.Stop();


Yes, run the sound file in a seperate thread which you start when the messagebox pops up. Once the MessageBox comes back with OK you can make a call into that thread telling the music to stop, or you could kill the thread.

EDIT

An alternative would be to write your own AlarmMessageBox which inherits from MessageBox. Its only difference would be that upon creation it would start playing that sound and upon clicking ok it would stop. This could work in just one thread.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜