Easy Notifications with System.Media.SystemSounds

I was working on a console application the other day that very commonly took several minutes (or in some cases, hours) to run to completion.

Right before terminating, it would print out a little summary of what it had done.  Instead of sitting there staring at the console waiting for my program to finish, I would start it up, work on something else for a little while, and periodically pull up the console to see if the program had finished.

After many instances of pulling up the console only to see it had not yet finished, I started to think about how I could have the application notify me upon termination.  I wanted to stop having to check in on it every couple minutes and just have it let me know when it was done.

Knowing that many applications that occupy the taskbar notification area (or the "system tray") often use a standard Windows sound to notify the user of events, I began looking for something easy I could do from C# to get a similar effect for my console application.

I had to look no further than System.Media.SystemSounds to get what I wanted.

Right before my program is set to terminate, I simply added the line:

System.Media.SystemSounds.Beep.Play();

Now my console application emits a standard Windows "ding" right before it terminates, and I avoid having to constantly check in on its progress.  Problem solved!

 

Note: You can also try Console.WriteLine('\a') to write out the ASCII control character known as the "alert" or "bell," but I wouldn't recommend it.  I scared myself half to death doing this, as the sound you'll hear is not regulated by the Windows volume setting and will likely be very loud and unexpected. :)

0 comments :