本文介绍了如何将Citrix中的C#程序实例限制为每用户1个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有C#代码的Windows Forms应用程序,如下所示(针对.NET Framework 4).

I have a Windows Forms application with C# code as shown below (targeting .NET framework 4).

在我的开发人员工作站上,此代码可防止我启动该程序的多个实例.但是,质量检查具有Citrix测试环境,每个用户仍然可以启动多个实例.

On my developer workstation, this code works to prevent me from launching multiple instances of the program. However, QA has a Citrix test environment where each user is still able to launch multiple instances.

如何防止给定用户在Citrix中运行多个实例?

What can be done to prevent a given user from running multiple instances in Citrix?

[STAThread]
static void Main(string[] args)
{
    bool isFirstInstance;
    Mutex m = new Mutex(true, "[App name goes here] mutex", out isFirstInstance);

    if (isFirstInstance)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run();

        // Prevent the just-in-time (JIT) compiler from optimizing away our Mutex.
        // See: http://www.ai.uga.edu/mc/SingleInstance.html
        GC.KeepAlive(m);
    }
}

出于技术原因,我们希望限制实例数量.该程序使用自托管的WCF与同一用户正在运行的另一个进程进行通信.我们只希望每个用户一个该程序的实例.

We want to limit the number of instances for technical reasons. The program uses self-hosted WCF to communicate with another process being run by the same user. We only want one instance of this program per user.

我不知道有关Citrix环境的任何详细信息,但可以查询.

I don't know any details about the Citrix environment, but can inquire.

谢谢.

推荐答案

根据所需的行为,使用本地或全局范围的互斥体可能是合适的.

Using either a Local or Global scoped mutex can be appropriate depending on exactly what behaviour you want.

将互斥锁与"Local \"一起使用将确保每个会话仅运行一个实例.但是,您的用户仍然有可能在同一服务器上启动多个会话(取决于Citrix环境的配置方式),从而使您的应用程序的多个实例在不同的会话中运行.

Using a mutex with "Local\" will ensure you only have one instance running per session. However it will still be possible for your user to launch multiple sessions on the same server (depending on how your Citrix environment is configured), and hence have multiple instances of your app running in different sessions.

如果要成为100%,每个用户每个服务器只能有一个实例,则需要使用全局互斥锁.但是,您需要确保使用特定于用户的状态来命名互斥锁,例如

If you want to be 100% each user only has once instance per server then you need to use a Global mutex. However you need to make sure you name your mutex with state specific to the user, e.g.

string globalMutexName = string.Format(
    CultureInfo.InvariantCulture,
    "Global\\AppName~{0}~{1}~some-unique-guid",
    Environment.UserDomainName,
    Environment.UserName);

_machineLocalAppInstanceMutex = new System.Threading.Mutex(true, globalMutexName, out mutexIsNew);

if (!mutexIsNew)
{
    Shutdown();
}

我还将互斥锁作为类的成员,通常是您的主App/Form类,而不是使用GC.KeepAlive

Also I'd make the mutex a member of a class, typically your main App/Form class rather than using GC.KeepAlive

这篇关于如何将Citrix中的C#程序实例限制为每用户1个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 01:53