(Not logged on) | Register | Log On

You can subscribe to this discussion group using an RSS feed reader. .NET Questions

Questions and Answers on any aspect of .NET.

This discussion group is now closed.

Have a question about .NET development? Try stackoverflow.com, a worldwide community of great developers asking and answering questions 24 hours a day.

The archives of .NET Questions contain years of Q&A. Even older .NET Questions are still online, too.

Single Instance of a application

Hi i have written a small utility program using c# when user clicks the exe as many times the new instances of the application get created what if i want the user to limit to use only one instance application at a time.... is there any way i can achieve it in C#..

Thankyou,
New To .Net
Tuesday, February 13, 2007
 
 
In terms of pure Win32 API you can use the CreateMutex Win32 API and use GetLastError to check for ERROR_ALREADY_EXISTS to detect any previous running instance.
Jussi Jumppanen
Tuesday, February 13, 2007
 
 
New To .Net
Tuesday, February 13, 2007
 
 
Sells' book has something on this, the relevant part of which is fortunately online:

http://www.awprofessional.com/articles/article.asp?p=474650&seqNum=2&rl=1

Never done it before though.
el
Wednesday, February 14, 2007
 
 
System.Threading.Mutex

See Also:
http://www.yoda.arachsys.com/csharp/faq/#one.application.instance

Snippet:
bool firstInstance;
Mutex mutex = new Mutex(false, "Local\\"+someUniqueName, out firstInstance);
// If firstInstance is now true, we're the first instance of the application;
// otherwise another instance is running.
Richard P
Wednesday, February 14, 2007
 
 
Which version of the CLR are you using? WinForms 2.0 has support for single instance apps built in.
Chris Tavares Send private email
Thursday, February 15, 2007
 
 
Only for Visual Basic applications, though.
Chris Nahr Send private email
Friday, February 16, 2007
 
 
Of course, you could always create a single-instance VB .NET project that runs your C# code.
Kyralessa Send private email
Friday, February 16, 2007
 
 
Chris, did you read it? The example is written in C#, you just add a reference to Microsoft.VisualBasic.dll, part of the .NET install.
el
Friday, February 16, 2007
 
 
You're right, judging by Sells' example it looks simple enough to use this functionality from C#.  The MSDN Library pages I found were talking about a VB project setting, that's why I thought it was an "exclusive" feature.
Chris Nahr Send private email
Friday, February 16, 2007
 
 
He does say he doesn't understand why the functionality wasn't added to the main framework (one of the notes, had to check in the book to find it). Although the dll is part of the distribution.
el
Friday, February 16, 2007
 
 
http://www.codeproject.com/csharp/singleinstance.asp


Guyyzzzzzzzzzzzzzz this is the superb example which works very fine check it out :)
New To .Net
Monday, February 19, 2007
 
 
Just remember, if you use a global atom, as the code project article suggests, if the program crashes the user will not be able to restart the application until they restart the computer and clear the globla atom.

But if you use a Mutex this is never a problem since windows will automatically release the mutex should the application crash.
Jussi Jumppanen
Tuesday, February 27, 2007
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz