469,626 Members | 1,046 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

Multi-instance vs single instance application

I have a general question.

If I write a application using any language(C/C++/C#/VB).
How to make sure it only has one instance running? What I
need to write in source code? If multi-instances run. Is
any data confilct exsisted? Assuming I put the .exe in
shared network drive and the multi-instances run on
different PC.

How about the multi-instances run on same PC?

How to make sure the exe can run multi instances.
Nov 15 '05 #1
3 2980
Kathy,

If you place the EXE on a network share and then have multiple PCs run
it, you don't have to worry about data in the application being shared
between instances. The binary will be loaded to each individual machine.
However, because it is from a network share, you have to make sure that you
configure it correctly to deal with security (the exe will run with a lower
permission set).

If you want only one instance of an application to run on a machine,
then you should place the following in your Main function:

// The entry point for the app.
public static void Main(string[] args)
{

// Tells whether or not the mutex was acquired.
bool pblnMutexAcquired = false;

// Create a mutex that will lock out access. It should be a named
instance. I prefer to
// use the full assembly name.
using (Mutex pobjMutex = new Mutex(true,
Assembly.GetExecutingAssembly().FullName, out pblnMutexAcquired))
{
// If the mutex was acquired, then run the app.
if (pblnMutexAcquired)
// Run the app.
Application.Run(new Form1());
}

// That's all folks.
return;
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"kathy" <an*******@discussions.microsoft.com> wrote in message
news:02****************************@phx.gbl...
I have a general question.

If I write a application using any language(C/C++/C#/VB).
How to make sure it only has one instance running? What I
need to write in source code? If multi-instances run. Is
any data confilct exsisted? Assuming I put the .exe in
shared network drive and the multi-instances run on
different PC.

How about the multi-instances run on same PC?

How to make sure the exe can run multi instances.

Nov 15 '05 #2
What an elegant solution! Thanks for posting this.

Erik

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2***************@TK2MSFTNGP11.phx.gbl...
Kathy,

If you place the EXE on a network share and then have multiple PCs run
it, you don't have to worry about data in the application being shared
between instances. The binary will be loaded to each individual machine.
However, because it is from a network share, you have to make sure that you configure it correctly to deal with security (the exe will run with a lower permission set).

If you want only one instance of an application to run on a machine,
then you should place the following in your Main function:

// The entry point for the app.
public static void Main(string[] args)
{

// Tells whether or not the mutex was acquired.
bool pblnMutexAcquired = false;

// Create a mutex that will lock out access. It should be a named
instance. I prefer to
// use the full assembly name.
using (Mutex pobjMutex = new Mutex(true,
Assembly.GetExecutingAssembly().FullName, out pblnMutexAcquired))
{
// If the mutex was acquired, then run the app.
if (pblnMutexAcquired)
// Run the app.
Application.Run(new Form1());
}

// That's all folks.
return;
}

Nov 15 '05 #3
Nicholas,

I hope you don't mind me jumping on this thread. I'm reading through
documentation on mutexes now and it says "Mutexes can be passed across
AppDomain boundaries."

I assume this is what is happening in your example code, because you are
running two instances of the same assembly, thus two appdomains, and yet
somehow both instances are referring to the same mutex, correct? Does this
mean that mutexes can be accessed by many different threads and AppDomains,
as long as they all originate from the same assembly? Or can mutexes be
passed between assemblies as well?

Thanks,

Erik

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2***************@TK2MSFTNGP11.phx.gbl...
Kathy,

If you place the EXE on a network share and then have multiple PCs run
it, you don't have to worry about data in the application being shared
between instances. The binary will be loaded to each individual machine.
However, because it is from a network share, you have to make sure that you configure it correctly to deal with security (the exe will run with a lower permission set).

If you want only one instance of an application to run on a machine,
then you should place the following in your Main function:

// The entry point for the app.
public static void Main(string[] args)
{

// Tells whether or not the mutex was acquired.
bool pblnMutexAcquired = false;

// Create a mutex that will lock out access. It should be a named
instance. I prefer to
// use the full assembly name.
using (Mutex pobjMutex = new Mutex(true,
Assembly.GetExecutingAssembly().FullName, out pblnMutexAcquired))
{
// If the mutex was acquired, then run the app.
if (pblnMutexAcquired)
// Run the app.
Application.Run(new Form1());
}

// That's all folks.
return;
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by OutsiderJustice | last post: by
37 posts views Thread by ajikoe | last post: by
4 posts views Thread by Frank Jona | last post: by
23 posts views Thread by Kaz Kylheku | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.