Geeks With Blogs
Saqib Ullah BootStrapper Know How

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 .Net | Back to top


Comments on this post: .Net Single Instance by using Mutex

# re: .Net Single Instance by using Mutex
Requesting Gravatar...
When you publish endpoints using WCF they are by default singletons I believe.
Left by Jonathan Starr on Dec 30, 2007 3:47 AM

# re: .Net Single Instance by using Mutex
Requesting Gravatar...
Isnt there an option in Project Properties to select Single Instance. And it does it automatically for you.
Left by MarkK on Dec 30, 2007 5:34 AM

# re: .Net Single Instance by using Mutex
Requesting Gravatar...
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.
Left by Aneel on Jan 30, 2008 1:29 AM

# re: .Net Single Instance by using Mutex
Requesting Gravatar...
Another way is :

include System.Diagnostics;

and check this :

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

Simple !
Left by Jolly Wagoner on Apr 03, 2008 5:36 AM

# re: .Net Single Instance by using Mutex
Requesting Gravatar...
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.
Left by Sachin on May 25, 2009 11:34 PM

# re: .Net Single Instance by using Mutex
Requesting Gravatar...
Saqib Ullah
BootStrapper Know How
Home | Contact | Syndication | Login
93 Posts | 1 Stories | 383 Comments | 15 Trackbacks

News


Article Categories
MS Servers
Archives
August, 2009 (2)
November, 2008 (2)
August, 2008 (2)
July, 2008 (1)
June, 2008 (3)
May, 2008 (2)
April, 2008 (1)
February, 2008 (2)
January, 2008 (4)
December, 2007 (4)
November, 2007 (3)
October, 2007 (1)
September, 2007 (7)
August, 2007 (4)
July, 2007 (9)
June, 2007 (9)
May, 2007 (5)
April, 2007 (11)
March, 2007 (3)
January, 2007 (4)
December, 2006 (7)
November, 2006 (4)
October, 2006 (3)
Post Categories
Exceptions (rss)
OOPs (rss)
.Net (rss)
General (rss)
Microsoft Servers (rss)
Security (rss)
Web Tech (rss)
SharePoint Service (rss)
IronPython (rss)
SOA (rss)
C++ (rss)
Blogging websites
asp
Blogspot
Geeks
Leads
msdn
Favourite Blogs
ado
brada
Brain
CLR GURU
Hamza Khalid
infosys
J.D. Meier's
jmstall
Khurram's
Mohtashim
MVP
nick malik
Noman Khan
ScottGu's
shawnfa
Private Links
Catch Me
Sites
Good Site for gulf New
.Net Single Instance by using Mutex
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:
Remember Me?
Powered by:

Skin design by Mark Wagner, Adapted by David Vidmar

Copyright © Saqib Ullah
Left by sr on Mar 06, 2010 2:20 AM

# re: .Net Single Instance by using Mutex
Requesting Gravatar...
use this......

Process.Start(path of ur exe);
Process[] processes = Process.GetProcessesByName(name of exe);
foreach (Process p in processes)
{
p.CloseMainWindow();
}
Left by hitesh on Mar 22, 2010 5:22 AM

# re: .Net Single Instance by using Mutex
Requesting Gravatar...
How can we open already running application using Mutex ???
Left by josey on Dec 15, 2010 2:50 AM

# re: .Net Single Instance by using Mutex
Requesting Gravatar...
very nice.. your post helped me..
Left by Kaos Muslim on Nov 16, 2011 11:17 PM

# re: .Net Single Instance by using Mutex
Requesting Gravatar...
boolean canRun=false;
string appName = "CustomDictionary";

System.Threading.Mutex m = new System.Threading.Mutex(true, appName, out canRun);
if (canRun)
{
Application.EnableVisualStyle();
Application.SetCompatibleTextRenderingDefault(false);
MainMdi MdiMain = new MainMdi();
Application.Run(MdiMain);
//--Tell the Garbage collector not to dispose this mutex
GC.KeepAlive(mutex);
}
else
{
MessageBox.Show("Application is already running.","Caption", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Left by M.Sherjil Khaliq on Apr 23, 2012 12:02 AM

Your comment:
 (will show your gravatar)


Copyright © Saqib Ullah | Powered by: GeeksWithBlogs.net