keyongtech


  keyongtech > dotnet.languages.* > dotnet.languages.csharp > 01/2004

 #1  
01-07-04, 07:18 PM
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.
 #2  
01-07-04, 07:44 PM
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.
 #3  
01-07-04, 11:26 PM
Erik Frey
What an elegant solution! Thanks for posting this.

Erik

"Nicholas Paldino [.NET/C# MVP]" <mvp> wrote in
message news:a556
[..]
 #4  
01-07-04, 11:53 PM
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

"Nicholas Paldino [.NET/C# MVP]" <mvp> wrote in
message news:a556
[..]
Similar Threads
single instance VS multi instance

anyone have any input on this? are they any performance hits or gains with one over the other. I'm looking for comparative numbers on a server load for example...say a test...

Single-instance app: how to force running instance window to foreground?

I've implemented single-instance functionality in my .exe by using the mutex method. Works great. But when the .exe detects that it is not the first instance I want to...

Single Instance of an Application

Hi, this is a basic question. I've to ensure than if an application is running, not to instance an oper anothier one and try to open or show the running application. How...

Custom Taglib problems - instead of a single instance per page, I have a single instance per application.

Hi, What is the correct expected behaviour when using a taglib regarding how many objects are created per page/application? More clearly, I created a custom tag (call it...

How to upgrade from single instance to multi instances

Hi, I have a SQL2K cluster (with Windows 2000 Advanced Server) with a single instance. I would like to add a second instance, and I'm asking what are the correct steps to...


All times are GMT. The time now is 11:20 PM. | Privacy Policy