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

后端 未结 4 1074
抹茶落季
抹茶落季 2021-01-02 13:42

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 mult

4条回答
  •  甜味超标
    2021-01-02 14:05

    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)

提交回复
热议问题