Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check for a running process per user session?

Tags:

c#

.net

I have a .NET application that I only allow to run a single process at a time of, however that app is used on Citrix boxes from time to time, and as such, can be run by multiple users on the same machine.

I want to check and make sure that the application is only running once per user session, because right now if user A is running the app, then user B gets the "App already in use" message, and should not.

This is what I have now that checks for the running process:

Process[] p = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
            if (p.Length > 1)
            {
#if !DEBUG
                allowedToOpen &= false;
                errorMessage +=
                    string.Format("{0} is already running.{1}", Constants.AssemblyTitle, Environment.NewLine);
#endif
            }
like image 426
Russ Avatar asked Apr 22 '09 15:04

Russ


People also ask

How can I see what processes are running for a particular user?

To list currently running processes, use the ps , top , htop , and atop Linux commands. You can also combine the ps command with the pgrep command to identify individual processes.

How can I see what processes are running command-line?

To view the list of the processes that are currently running, you can use the tasklist command, both in Command Prompt and PowerShell. Type tasklist and press Enter.


3 Answers

EDIT: Improved the answer according to this cw question ...

You can use a mutex for checking wether the app already runs:

using( var mutex = new Mutex( false, AppGuid ) )
{
    try
    {
        try
        {
            if( !mutex.WaitOne( 0, false ) )
            {
                MessageBox.Show( "Another instance is already running." );
                return;
            }
        }
        catch( AbandonedMutexException )
        {
            // Log the fact the mutex was abandoned in another process,
            // it will still get aquired
        }

        Application.Run(new Form1());
    }
    finally
    {
        mutex.ReleaseMutex();
    }
}

Important is the AppGuid - you could make it depend on the user.

Maybe you like to read this article: the misunderstood mutex

like image 173
tanascius Avatar answered Nov 01 '22 15:11

tanascius


As tanascius already say, you can use the Mutex.

On a server that is running Terminal Services, a named system mutex can have two levels of visibility. If its name begins with the prefix "Global\", the mutex is visible in all terminal server sessions. If its name begins with the prefix "Local\", the mutex is visible only in the terminal server session where it was created.

Source: msdn, Mutex Class

like image 30
robertpnl Avatar answered Nov 01 '22 15:11

robertpnl


Just stating the obvious - although Mutex is usually considered better solution, you can still solve the single-instance-per-session issue without Mutex - just test the SessionId as well.

    private static bool ApplicationIsAlreadyRunning()
    {
        var currentProcess = Process.GetCurrentProcess();
        var processes = Process.GetProcessesByName(currentProcess.ProcessName);

        // test if there's another process running in current session.
        var intTotalRunningInCurrentSession = processes.Count(prc => prc.SessionId == currentProcess.SessionId);

        return intTotalRunningInCurrentSession > 1;
    }

Source (no Linq)

like image 44
itsho Avatar answered Nov 01 '22 16:11

itsho