gpt4 book ai didi

c# - 跨用户 C# 互斥

转载 作者:IT王子 更新时间:2023-10-29 04:29:15 26 4
gpt4 key购买 nike

我的应用程序被迫使用第 3 方模块,如果在同一台机器上同时启动两个实例,该模块将使 Windows 蓝屏。为了解决这个问题,我的 C# 应用程序有一个互斥量:

static Mutex mutex = new Mutex(true, "{MyApp_b9d19f99-b83e-4755-9b11-d204dbd6d096}");  

然后我检查它是否存在 - 如果存在,我会显示一条错误消息并关闭应用程序:

bool IsAnotherInstanceRunning()
{
if (mutex.WaitOne(TimeSpan.Zero, true))
return (true);
else
return (false);
}

问题是如果两个用户可以同时登录并打开应用程序,IsAnotherInstanceRunning() 将返回 false。

我该如何解决这个问题?

最佳答案

用“Global\”作为互斥体名称的前缀。

static Mutex mutex = new Mutex(true, "Global\MyApp_b9d19f99-b83e-4755-9b11-d204dbd6d096");  

来自 http://msdn.microsoft.com/en-us/library/system.threading.mutex.aspx :

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. In that case, a separate mutex with the same name can exist in each of the other terminal server sessions on the server. If you do not specify a prefix when you create a named mutex, it takes the prefix "Local\".

关于c# - 跨用户 C# 互斥,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2830546/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com