While I was developing Auto CCleaner I faced a problem with allowing only one instant of the application. I found many solutions (using mutex) and others, but it didn’t work or it was inefficient. What I needed is a simple, easy, and efficient method. Hence I used little help from .NET Process class as follow:
I added this block to initialize (load) function:
1 2 3 4 5 |
if (Process.GetProcessesByName(Application.ProductName).GetLength(0) >= 2) { MessageBox.Show("Only one instance of the app is allowed!"); this.Close(); } |
Tada! Only a single instance is allowed now.
_
note: You need to add: using System.Diagnostics;
at the beginning of your cs
file.
Recent Comments