gpt4 book ai didi

c# - 多个 Windows 用户帐户上的一个实例应用程序

转载 作者:太空狗 更新时间:2023-10-29 23:16:28 25 4
gpt4 key购买 nike

我已经使用 Mutex 的解决方案来禁止同时打开我的应用程序的多个实例

但是,当我在 windows 中使用 Switch User 并打开另一个用户,并尝试打开该应用程序时,它正常打开并且无法检测到它正在其他帐户上运行

我怎样才能解决这样的问题并禁止在登录的 Windows 用户帐户中打开应用程序

我正在使用这样的代码

Dim onlyInstance As Boolean = False
_mutex = New Mutex(True, "Application Name", onlyInstance)
If Not onlyInstance Then
MessageBox.Show("Application is already running.", "Error.")
System.Diagnostics.Process.GetCurrentProcess.Kill()
System.Diagnostics.Process.GetCurrentProcess.WaitForExit()
End If

最佳答案

如果你让你的互斥量成为全局的,那么它对所有用户都是可见的:

mutexName = String.Format("Global\\{{{0}}}", "Application Name"); 

来自documentation :

A named system mutex can have two levels of visibility. 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\". Within a terminal server session, two mutexes whose names differ only by their prefixes are separate mutexes, and both are visible to all processes in the terminal server session. That is, the prefix names "Global\" and "Local\" describe the scope of the mutex name relative to terminal server sessions, not relative to processes.

关于c# - 多个 Windows 用户帐户上的一个实例应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12898616/

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