Prevent multiple instances in server same username

前端 未结 2 1900
误落风尘
误落风尘 2021-01-18 22:13

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 ha

相关标签:
2条回答
  • 2021-01-18 22:56

    The recommended way to see if another instance of your application is already running is to use a Mutex. See here for example.

    Since you want to allow multiple instances of the application to run if different users run them, simply add the current user name to the mutex's name. For example, call the Mutex "MyApp"+Environment.UserName

    0 讨论(0)
  • 2021-01-18 22:58

    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");
    
    0 讨论(0)
提交回复
热议问题