繁体   English   中英

如何限制用户打开多个exe实例

[英]How to restrict user from opening more than one instance of the exe

我的应用程序以exe的形式发布在两个构建版本中-DeveloperBuild和ClientBuild(UAT)。 DeveloperBuild适用于内部开发人员和质量检查测试,而ClientBuild适用于最终客户。 “ DeveloperBuild”和“ ClientBuild”实际上是程序集名称。

我想限制用户打开一个以上的构建实例。简单来说,用户应该能够同时打开一个DeveloperBuild实例和一个ClientBuild实例,但不允许用户打开一个以上的实例。同时开发DeveloperBuild或ClientBuild。

这就是我尝试过的。 下面的代码可以帮助我维护应用程序的单个实例,但是不能区分Developer Build和Client Build。 我希望用户具有同时打开两个构建的单个实例的优势。

///应用程序的入口点

    protected override void OnStartup(StartupEventArgs e)
    {           
        const string sMutexUniqueName = "MutexForMyApp";

        bool createdNew;

        _mutex = new Mutex(true, sMutexUniqueName, out createdNew);

        // App is already running! Exiting the application  
        if (!createdNew)
        {               
            MessageBox.Show("App is already running, so cannot run another instance !","MyApp",MessageBoxButton.OK,MessageBoxImage.Exclamation);
            Application.Current.Shutdown();
        }

        base.OnStartup(e);

        //Initialize the bootstrapper and run
        var bootstrapper = new Bootstrapper();
        bootstrapper.Run();
    }

互斥名称在每个版本中必须唯一。 因为每个版本都有不同的程序集名称,所以可以在互斥体名称中包含此名称,如下所示。

protected override void OnStartup(StartupEventArgs e)
{           
    string sMutexUniqueName = "MutexForMyApp" + Assembly.GetExecutingAssembly().GetName().Name;

    bool createdNew;

    _mutex = new Mutex(true, sMutexUniqueName, out createdNew);

    // App is already running! Exiting the application  
    if (!createdNew)
    {               
        MessageBox.Show("App is already running, so cannot run another instance !","MyApp",MessageBoxButton.OK,MessageBoxImage.Exclamation);
        Application.Current.Shutdown();
    }

    base.OnStartup(e);

    //Initialize the bootstrapper and run
    var bootstrapper = new Bootstrapper();
    bootstrapper.Run();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM