Saqib Ullah

BootStrapper Know How

  Home  |   Contact  |   Syndication    |   Login
  109 Posts | 1 Stories | 820 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);
 } 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
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 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.

# re: .Net Single Instance by using Mutex 3/6/2010 2:20 AM sr
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

# re: .Net Single Instance by using Mutex 3/22/2010 5:22 AM hitesh
use this......

Process.Start(path of ur exe);
Process[] processes = Process.GetProcessesByName(name of exe);
foreach (Process p in processes)
{
p.CloseMainWindow();
}

# re: .Net Single Instance by using Mutex 12/15/2010 2:50 AM josey
How can we open already running application using Mutex ???

# re: .Net Single Instance by using Mutex 11/16/2011 11:17 PM Kaos Muslim
very nice.. your post helped me..

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