Identify multiple running instances of an application

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, I wrote an app in .Net and I whant only 1 instance of this app open for
the user; the user open my app, do some works and try to open another
instance of my app, I whant to show a message to user to inform him that only
one instance is permit and then close the second instance after that.

I am able to do this when the user run the application on his PC whit this :

Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName)

and if I found the name of my app in the returned collection, I display the
message and close the seconde instance.

The problem now: if my application is used by Remote session on a central PC
who others users can also open my application at the same time, I would like
to let the others users open my app. The only one contrainte is one instance
by user. The code I used does not work in this scenario.

Does anyone have idea to solution this ?

Thanks a lot
Michel
 
If you know the name of the process you can do something like this

using System.Diagnostics;
if (Process.GetProcessesByName(processName).Length > 0)
return true;
// where processName is the process you are looking for.


The other option is; you can use a Mutex and verify if the Mutex already
exists.

--------------------
Thread-Topic: Identify multiple running instances of an application
thread-index: AcZGp1JCRsO2yAK6TdOQEauDPYg6Nw==
X-WBNR-Posting-Host: 66.38.186.253
From: =?Utf-8?B?TWljaGVs?= <[email protected]>
Subject: Identify multiple running instances of an application
Date: Mon, 13 Mar 2006 06:06:28 -0800
Lines: 21
Message-ID: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain;
charset="Utf-8"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.1830
Newsgroups: microsoft.public.dotnet.general
Path: TK2MSFTNGXA03.phx.gbl
Xref: TK2MSFTNGXA03.phx.gbl microsoft.public.dotnet.general:191032
NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
X-Tomcat-NG: microsoft.public.dotnet.general

Hi, I wrote an app in .Net and I whant only 1 instance of this app open for
the user; the user open my app, do some works and try to open another
instance of my app, I whant to show a message to user to inform him that only
one instance is permit and then close the second instance after that.

I am able to do this when the user run the application on his PC whit this :

Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName)

and if I found the name of my app in the returned collection, I display the
message and close the seconde instance.

The problem now: if my application is used by Remote session on a central PC
who others users can also open my application at the same time, I would like
to let the others users open my app. The only one contrainte is one instance
by user. The code I used does not work in this scenario.

Does anyone have idea to solution this ?

Thanks a lot
Michel

--

Thank You,
Nanda Lella,

This Posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Nanda, thank you for your answer. Your code will work but the problem I
have is how to find the process used by a specific user. Many users can
connect by remote sessions on a computer to run my application. I whant to
restrict 1 instance by user. The code you suggest to me can find the process
(my app) of another user connected remotly to the computer, and I don't whant
it... is it possible to do this ? I don't know.... :(

thank you for your help....

Michel
 
Hello Michel,

If you use Mutex then you can restrict a single instance per user.
Also you can create a global Mutex to restrict single instance per machine.

using System.Threading;
Mutex processMutex = new Mutex(false,"Your Application Name");
if (processMutex.WaitOne(0, false) == false)
{
//Multiple intstance detected.
}//if



//Approach 2
using System.Threading;
bool isFirstInstance;
Mutex processMutex = new Mutex(true, "Your Application Name", out
isFirstinstance);
if(!isFirstInstance)
{
//Multiple instance detected.
}//

//If you plan to do one instance per machine create the mutex this way
Mutex processMutex = new Mutex(true, "Global\\" + "Your Application Name",
out isFirstinstance);



The important point to note is dont forget to release/dispose the mutex
when there is an error or when you are done with the applications. Best
place is to do the mutex release in the finally block.


if (processMutex != null)
{
processMutex.Close();
processMutex = null;
}//if


Hope this helps,


--------------------
Thread-Topic: Identify multiple running instances of an application
thread-index: AcZHbJn8ROGT6iGXT1KtfXVgY7xLYQ==
X-WBNR-Posting-Host: 66.38.186.253
From: =?Utf-8?B?TWljaGVs?= <[email protected]>
References: <[email protected]>
Subject: RE: Identify multiple running instances of an application
Date: Tue, 14 Mar 2006 05:38:39 -0800
Lines: 86
Message-ID: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain;
charset="Utf-8"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.1830
Newsgroups: microsoft.public.dotnet.general
Path: TK2MSFTNGXA03.phx.gbl
Xref: TK2MSFTNGXA03.phx.gbl microsoft.public.dotnet.general:191137
NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
X-Tomcat-NG: microsoft.public.dotnet.general

Hi Nanda, thank you for your answer. Your code will work but the problem I
have is how to find the process used by a specific user. Many users can
connect by remote sessions on a computer to run my application. I whant to
restrict 1 instance by user. The code you suggest to me can find the process
(my app) of another user connected remotly to the computer, and I don't whant
it... is it possible to do this ? I don't know.... :(

thank you for your help....

Michel

--

Thank You,
Nanda Lella,

This Posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top