0

I have a c#/WPF application that on some clients sites runs over RPD i.e. the application resides on the clients server and users access through terminal services sessions.

The problem I'm having is that I can't prevent multiple instances of the application opening accidently. When the application is running locally on a machine I can block out multiple instances using the following:

var processName = Process.GetCurrentProcess().ProcessName;   
                if (Process.GetProcesses().Count(p => p.ProcessName.Equals(processName)) > 1)
                {
                    this.Log.LogInfo(this.GetType(), "Process already running. Shutting down.");
                    Application.Current.Shutdown();
                    Process.GetCurrentProcess().Kill();
                }    

However, over RPP this won't work as there may be other instances of the application running on different RDP sessions.

Anybody know how I can disable a second application instance running in the same RDP session?



Saoirse
  • 237
  • 2
  • 16

2 Answers2

1

In the past I've accomplished this using a Mutex. When creating the mutex you would use the naming convention Global\\MutexName to handle terminal services scenarios.

// declare in your program
private static Mutex mutex = null;

bool createdNew;  
mutex = new Mutex(true, "Global\\MutexName", out createdNew); 

if (!createdNew)
{
    // Application is already running, so shutdown
    Application.Current.Shutdown();     
}
Garett
  • 16,632
  • 5
  • 55
  • 63
  • 1
    I am doing something similar and I am running my apps in Terminal Services. I am using this http://blogs.microsoft.co.il/arik/2010/05/28/wpf-single-instance-application/ – MrZander Oct 16 '18 at 19:56
0

I guess every remote connection will be from another user. You may use the following to determine who's the owner of the process: How do I determine the owner of a process in C#?

Joel Bourbonnais
  • 2,618
  • 3
  • 27
  • 44