Question

In a terminal server situation I want to make sure only 1 instance of the application runs for each user.

What is the best way to do this?

Here is what i'm currently using but it seems like it isn't working as intended. I can't be 100% on that.

int SID = Process.GetCurrentProcess().SessionId;

Process current_process = Process.GetCurrentProcess();
int proc_count = 0;

foreach (Process p in Process.GetProcessesByName(current_process.ProcessName))
{
    proc_count++;
    if (p.SessionId.Equals(SID))
    {
        if (proc_count > 1)
        {
            Application.Current.Shutdown();
            return;
        }
    }
}

UPDATE To help clarify by the comments I have read I think it needs to be off the session ID because not all clients will follow one physical person per username and there for the user can be logged into the server multiple times.

Was it helpful?

Solution

The generally accepted way to do this is with a named mutex. By creating a named mutex you can determine if another instance is already running and shutdown the new instance.

This code should give you the desired result. All of this is assumed to be in the executable's main entry point.

bool createdMutex;
using (var processMutex = new System.Threading.Mutex(false, "Some name that is unique for the executable", out createdMutex)) {

   if (!createdMutex)
     ; // some other instance of the application is already running (or starting up). You may want to bring the other instance to foreground (assuming this is a GUI app)
   else 
   {
      // this is the process to actually run..
      // do application init stuff here
   }
}

Be warned, I typed the code here, so there may be syntax errors, typos and other accidental misdirection.

As request in the comments, here is some direction on choosing the name of the mutex:

If you want to limit the application to a single instance per terminal server session, then choose a mutex name that is unique to the application.

If you want to limit the application to a single instance per "user" within a terminal server session, choose a mutex name that is unique to the application and append the user's SID. WindowsIdentity.GetCurrent() will provide you with the SecurityIdentifier.

If you want to limit the application to a single instance per user, regardless of which terminal server session, choose a mutex name this is unique to the application with the user's SID included and prefix the name with "Global\".

If you want to limit the application to a single instance per server, choose a mutex name that is unique to the application and prefix with "Global\".

It is important to note that while a terminal server session is associated with a single user, that does not mean that all of the applications running in the session are associated with that user (consider "Run As" options).

OTHER TIPS

put a row in the database status = 1 or 0

1 means he's on

0 means he's off

if(status==0)
{
    //let him in
}
else
{
    //dont let him into the system
}

hopes this help with the structure/logic cause im not really very familiar with c# or .net

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top