1

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?

Esoteric Screen Name
  • 6,082
  • 4
  • 29
  • 38
Rage
  • 31
  • 1
  • 6
  • This appears to be too broad a question because I don't know how to explain program sequence in a way that is generally applicable without taking seventeen pages to cover all the bases. – Nathan Tuggy Mar 14 '15 at 03:56

1 Answers1

1

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();
}
Esoteric Screen Name
  • 6,082
  • 4
  • 29
  • 38
  • If you want the application to exit only if the condition is `true` the above will work, but if you want the application to exit even if the condition is `false` but only have the sound play if `true`, then you need to move `Application.Exit()` outside the `if` statement. – Jacob Lambert Mar 14 '15 at 03:55