gpt4 book ai didi

c# - 如何检查每个用户 session 的运行进程?

转载 作者:太空狗 更新时间:2023-10-29 18:33:42 26 4
gpt4 key购买 nike

我有一个 .NET 应用程序,我一次只允许运行一个进程,但是该应用程序有时会在 Citrix boxes 上使用,因此,可以由同一台机器上的多个用户运行.

我想检查并确保该应用程序在每个用户 session 中只运行一次,因为现在如果用户 A 正在运行该应用程序,那么用户 B 会收到“应用程序已在使用中”的消息,而这不应该。

这是我现在检查正在运行的进程的内容:

Process[] p = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);
if (p.Length > 1)
{
#if !DEBUG
allowedToOpen &= false;
errorMessage +=
string.Format("{0} is already running.{1}", Constants.AssemblyTitle, Environment.NewLine);
#endif
}

最佳答案

编辑:根据 this cw question 改进了答案...

您可以使用互斥锁来检查应用是否已经运行:

using( var mutex = new Mutex( false, AppGuid ) )
{
try
{
try
{
if( !mutex.WaitOne( 0, false ) )
{
MessageBox.Show( "Another instance is already running." );
return;
}
}
catch( AbandonedMutexException )
{
// Log the fact the mutex was abandoned in another process,
// it will still get aquired
}

Application.Run(new Form1());
}
finally
{
mutex.ReleaseMutex();
}
}

重要的是 AppGuid - 你可以让它取决于用户。

也许你喜欢阅读这篇文章:the misunderstood mutex

关于c# - 如何检查每个用户 session 的运行进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/777744/

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