473,385 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Most robust way to implement single-instance Windows app?

Jen
What is the most robust way to implement single-instancing for a Windows
app? I'm aware of the GetProcessByName method and the Mutex method. I've
GetProcessByName isn't very robust. What happens if the app crashes and
it's using the Mutex method? I don't want it to get in a situation where
the Mutex prevents the app. from starting when it really is ok to start. Is
the Mutex method the most robust? Any others?

Using .NET 2.0 and C#.
Mar 26 '07 #1
9 5185
Jen,

If the app crashes, the Mutex should be disposed of, assuming that you
are using a using statement or a try/finally statement where you are calling
Dispose on the Mutex. Short of an error in the CLR, a regular exception in
this case will not cause the Mutex to hang around.

You might also want to check out this thread, which points to using the
WindowsApplicationBase class in the Microsoft.VisualBasic namespace which
handles exactly what you are looking for.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Jen" <no**@nowhere.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
What is the most robust way to implement single-instancing for a Windows
app? I'm aware of the GetProcessByName method and the Mutex method. I've
GetProcessByName isn't very robust. What happens if the app crashes and
it's using the Mutex method? I don't want it to get in a situation where
the Mutex prevents the app. from starting when it really is ok to start.
Is the Mutex method the most robust? Any others?

Using .NET 2.0 and C#.

Mar 26 '07 #2
On Sun, 25 Mar 2007 20:26:46 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
If the app crashes, the Mutex should be disposed of, assuming that
you are using a using statement or a try/finally statement where you are
calling Dispose on the Mutex. Short of an error in the CLR, a regular
exception in this case will not cause the Mutex to hang around.
It should be disposed of regardless, since even in native Win32, Windows
will dispose of the application's handle to the mutex when the application
terminates (normally or abnormally).

Pete
Mar 26 '07 #3
Jen
I'm trying the Mutex method. It seems to work on my development system but
on a test machine it doesn't prevent multiple instances. I'm doing
something like this:

bool firstInstance;
Mutex mutex = new Mutex(false, "klsjflksdjfklsdjflkd", out firstInstance);
if (!firstInstance)
return;
Mar 26 '07 #4
Jen <no**@nowhere.comwrote:
I'm trying the Mutex method. It seems to work on my development system but
on a test machine it doesn't prevent multiple instances. I'm doing
something like this:

bool firstInstance;
Mutex mutex = new Mutex(false, "klsjflksdjfklsdjflkd", out firstInstance);
if (!firstInstance)
return;
See http://pobox.com/~skeet/csharp/faq/#...ation.instance

(The paragraph starting with "One thing to beware of" describes your
situation.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 26 '07 #5
"Jen" <no**@nowhere.comwrote in message news:%2****************@TK2MSFTNGP05.phx.gbl...
What is the most robust way to implement single-instancing for a Windows
app? I'm aware of the GetProcessByName method and the Mutex method. I've
GetProcessByName isn't very robust. What happens if the app crashes and
it's using the Mutex method? I don't want it to get in a situation where
the Mutex prevents the app. from starting when it really is ok to start. Is
the Mutex method the most robust? Any others?

Using .NET 2.0 and C#.


Do as Nicholas said and apply the using pattern on the Mutex...
...
bool firstInstance;
using (Mutex AppMutex = new Mutex(true, new
Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx").ToString(), out firstInstance))
{
if (firstInstance== true)
{
// run your program code from here
}
} // end using scope, mutex released here.
...

Willy.
Mar 26 '07 #6
On Mar 25, 7:07 pm, "Jen" <n...@nowhere.comwrote:
What is the most robust way to implement single-instancing for a Windows
app? I'm aware of the GetProcessByName method and the Mutex method. I've
GetProcessByName isn't very robust. What happens if the app crashes and
it's using the Mutex method? I don't want it to get in a situation where
the Mutex prevents the app. from starting when it really is ok to start. Is
the Mutex method the most robust? Any others?

Using .NET 2.0 and C#.
A probable work around if you intend to avoid mutex, Give your main
form a unique name that has *good* chances of being unique and then
use the FindWindow API to retrieve its window handle.
You can check for the existence of the instance in the entry point of
your application and prevent from creating an instance if it is
already instantiated.

Raaj

Mar 26 '07 #7
Willy,
>new Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx").ToString()
Now there's an interesting way of generating a Guid string. :-)
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mar 26 '07 #8
On Mon, 26 Mar 2007 08:47:53 -0700, Raaj <ra***********@yahoo.comwrote:
A probable work around if you intend to avoid mutex, Give your main
form a unique name that has *good* chances of being unique and then
use the FindWindow API to retrieve its window handle.
You can check for the existence of the instance in the entry point of
your application and prevent from creating an instance if it is
already instantiated.
One problem with this method is that there is no way to guarantee one
instance of the application will have already created its window when the
next instance goes to check for it and vice a versa.

With the mutex, exactly one of the instances will actually create the
mutex, and the operating system guarantees exclusive access to the mutex.
There's no similar conflict resolution when checking for the application
window. The application can detect the case where the race condition
results in more than one instance running anyway, but then it still needs
to deal with deciding which instance "wins" and which instances have to
exit.

Basically, the mutex method is simple and provides guarantees that other
methods don't.

Pete
Mar 26 '07 #9
"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:OT****************@TK2MSFTNGP05.phx.gbl...
Willy,
>>new Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx").ToString()

Now there's an interesting way of generating a Guid string. :-)

Mattias,
LOL.
But seriously, at least it forces people to think about what it's meant for and what they
should do with it :-).

Willy.

Mar 27 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Martin Smith | last post by:
Hi, Does anyone know a good (c++)example of the implementation of a "robust" list iterator (ie an iterator whereby insert/delete operations does not have any effect on the list)? I am looking...
4
by: tkpmep | last post by:
I use Python to generate a huge amount of data in a .csv file which I then process using Excel. In particular, I use Excel's solver to solve a number of non-linear equation, and then regress the...
6
by: erebus- | last post by:
When learning the C programing languages, i have had and still am having the problem of not being able to find answers to many questions. Is their an overall guide/reference that someone knows?
11
by: hoopsho | last post by:
Hi Everyone, I am trying to write a program that does a few things very fast and with efficient use of memory... a) I need to parse a space-delimited file that is really large, upwards fo a...
62
by: Juuso Hukkanen | last post by:
I am looking for a wish list of things which should be removed from the C (C99) - due to feature's bad security track record <OT>or Multithreading unsafety. I need this list for a project intending...
0
by: Benjamin | last post by:
I am attempting to create multiple itterators for a custom class that I have created. Basically I have a class that I am populating using XML de-serialization. I want to be able to loop through the...
33
by: Matt Kruse | last post by:
I'm seeking the most robust and backwards-compatible (ie, no instanceof) isArray function. Here's what I have: function defined(o) { return typeof(o)!="undefined"; } function isArray(o) {...
2
by: Bjarne | last post by:
Dear all, although being a user of PHP and other scripting-languages for years, I have not taken part in any large projects based on PHP. Thus, I don't really know how real PHP-applications are...
19
by: UG | last post by:
I just wanted to know whether any timer facility exists in C, as it is not mentioned in K&R 2, or in the ISO Draft. By timer function i mean that when we use standard input function like scanf() or...
21
by: py_genetic | last post by:
Hello, I'm importing large text files of data using csv. I would like to add some more auto sensing abilities. I'm considing sampling the data file and doing some fuzzy logic scoring on the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.