Saturday, February 14, 2009

force one instance of an application in .NET

To force a single instance of an application, either fetch all processess and check your application process name that is it already there or not. Another option is to use Mutex class.

bool NoInstanceCurrently;
System.Threading.Mutex mutex = new System.Threading.Mutex(false, "applicationname", out NoInstanceCurrently);
if (NoInstanceCurrently == false)

MessageBox.Show("Another Instance is Running", "ApplicationName", MessageBoxButtons.OK, MessageBoxIcon.Stop);

Play sound from Window Form in C#

Playing a sound from window forms in C# is simple because of SoundPlayer class from System.Media namespace.

SoundPlayer objSoundPlayer = new SoundPlayer("sound1.wav");
objSoundPlayer .Play();