Friday, August 29, 2014

Play a sound when the application closes in C#


The first form you open has a special meaning - it's the mainwindow.


When you close that window the application closes.


In my windows forms application this is Form2.


I handle the FormClosing event on that window and it works OK for me:



private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(@"C:\Windows\Media\Alarm09.wav");

player.Play();
Application.DoEvents();
System.Threading.Thread.Sleep(1000);
}





Hope that helps

Please don't forget to up vote answers you like or which help you and mark one(s) which answer your question.



EDIT: It worked it the end - it seemed that the code needed to be placed on the startup form!




---- JDS404 ---- Check out my blog at http://ift.tt/SaWROI!



No comments:

Post a Comment