Reputation: 31
I am trying to make my application play a quick .wav before closing the application, but every time it only plays the .wav and does not exit the application. Here is my code:
bool condition = false;
if (condition)
{
SoundPlayer sndPlayer = new SoundPlayer(Project_Rage_v3._1.Properties.Resources.syl);
sndPlayer.Play();
}
else
{
Application.Exit();
}
I know if the bool == true it will do the if function and if the bool == false it will do the else function. But if I don't split them up, the program just quits immediately without playing the sound.
How do I have it play the sound then quit, but only quit after the sound is finished playing?
Upvotes: 1
Views: 885
Reputation: 6112
Remove the else block. You're telling the program to play the sound if condition
is true, and exit if condition
is false. Put the commands in sequential order and call PlaySync
instead of Play
(MSDN). This will block the rest of the code from executing until the playing is finished.
bool condition = false;
if (condition)
{
SoundPlayer sndPlayer = new SoundPlayer(Project_Rage_v3._1.Properties.Resources.syl);
sndPlayer.PlaySync();
Application.Exit();
}
Upvotes: 1