Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent multiple instances in server same username

Tags:

c#

winforms

I developed an application that is hosted in one server. Many users access to it via Remote Desktop connection, but sometimes I saw in the task manager that the same user has opened 2-x instances. I need prevent that the same user can't open multiple instances. But notice that the program can be opened multiple times by different users. Please forgive me my English. Thanks.

PS: I'm using Winforms and C#

like image 597
Yuri Morales Avatar asked Oct 21 '22 07:10

Yuri Morales


1 Answers

You can create a mutex with the user's name.

bool b = true;
Mutex mutex = new Mutex(true, Environment.UserName.ToLowerInvariant() , out b);
if (!b) throw new InvalidOperationException("Another instance is running");
like image 186
I4V Avatar answered Nov 15 '22 06:11

I4V