Multi-instance vs single instance application

K

kathy

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
E

Erik Frey

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top