【问题标题】:How to restrict user from opening more than one instance of the exe如何限制用户打开多个exe实例
【发布时间】:2018-11-07 00:16:03
【问题描述】:

我的应用程序在两个构建版本中作为 exe 发布 - DeveloperBuild 和 ClientBuild(UAT)。 DeveloperBuild 适用于内部开发人员和 QA 测试,而 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();
    }

【问题讨论】:

  • 哪些程序(环境)客户端用于构建和运行?用户使用什么操作系统?
  • @Fedor 环境是 Dev 和 UAT。使用的操作系统是 Windows。
  • 您是否尝试使用 Windows 注册表而不是 Mutex,通过它设置和获取值?

标签: c# .net build mutex semaphore


【解决方案1】:

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

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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    • 2013-06-01
    • 2012-01-05
    • 2019-09-03
    • 1970-01-01
    • 2017-04-13
    相关资源
    最近更新 更多