Saqib Ullah

BootStrapper Know How

  Home  |   Contact  |   Syndication    |   Login
  93 Posts | 1 Stories | 343 Comments | 15 Trackbacks

News



Article Categories

Archives

Post Categories

Blogging websites

Favourite Blogs

Private Links

Sites

Windows application developer some required that to run single instance of the application. I want to share with you geeks how I can do it in the .Net application. I don’t know how many of you guys know that this thing, but if you have another better way of doing this task so please give comments on blog.

Note: Please change the appName variable with your application name.

 bool exclusive;
string appName = "CustomDictionary";

  System.Threading.Mutex m =  new System.Threading.Mutex(true, appName, out exclusive);

  if (!exclusive)
  {
            MessageBox.Show("Another instance of " + Application.ProductName + " is running.",                     "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
 } 

posted on Sunday, December 30, 2007 2:09 AM

Feedback

# re: .Net Single Instance by using Mutex 12/30/2007 3:47 AM Jonathan Starr
When you publish endpoints using WCF they are by default singletons I believe.

# re: .Net Single Instance by using Mutex 12/30/2007 5:34 AM MarkK
Isnt there an option in Project Properties to select Single Instance. And it does it automatically for you.

# re: .Net Single Instance by using Mutex 1/30/2008 1:29 AM Aneel
Thanks for the article.
This seems to be the most optimal way of doing it. But I need some additional functionality to it. My requirement is to check if there is another instance of the application running and if yes, close the current instance and the set the focus to the existing instance.
It would be nice of you if can help me in acheiving this functionality.
Thanks in advance for the reply.


# re: .Net Single Instance by using Mutex 4/3/2008 5:36 AM Jolly Wagoner
Another way is :

include System.Diagnostics;

and check this :

if(Process.GetProcessesByName("Your application name").Length > 1 )
return;

Simple !

# re: .Net Single Instance by using Mutex 9/13/2008 5:45 PM Mp3 Film Program Download
Thanks o

# re: .Net Single Instance by using Mutex 5/25/2009 11:34 PM Sachin
if(Process.GetProcessesByName("Your application name").Length > 1 )
return;

The above code will work fine if you use only same Exe. You can run multiple instance by doing copy & paste the same exe with different name.

Scenario:

copy xyz.exe and paste in same folder, it will create new file by name "copy of xyz.exe", now if you run this file, it will run.

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: