【问题标题】:Prevent my windows application to run multiple times防止我的 Windows 应用程序运行多次
【发布时间】:2022-01-02 10:55:39
【问题描述】:

我在 Visual Studio 中构建了一个 Windows 应用程序,它将部署到其他 PC 上,有多个用户,我想阻止我的应用程序运行多次,有什么方法可以以编程方式阻止它?还是其他方式?

【问题讨论】:

    标签: c# visual-studio-2010 visual-studio-2019


    【解决方案1】:

    您可以为此目的使用命名互斥体。命名(!)互斥锁是系统范围的同步对象。我在我的项目中使用以下类(略微简化)。它在构造函数中创建一个最初无主的互斥体,并在对象生命周期内将其存储在成员字段中。

    public class SingleInstance : IDisposable
    {
      private System.Threading.Mutex  _mutex;
      
      // Private default constructor to suppress uncontrolled instantiation.
      private SingleInstance(){}
      
      public SingleInstance(string mutexName)
      {
        if(string.IsNullOrWhiteSpace(mutexName))
          throw new ArgumentNullException("mutexName");
        
        _mutex = new Mutex(false, mutexName);         
      }
    
      ~SingleInstance()
      {
        Dispose(false);
      }
        
      public bool IsRunning
      {
        get
        {
          // requests ownership of the mutex and returns true if succeeded
          return !_mutex.WaitOne(1, true);
        }    
      }
    
      public void Dispose()
      {
        GC.SuppressFinalize(this);
        Dispose(true);
      }
    
      protected virtual void Dispose(bool disposing)
      {
        try
        {
          if(_mutex != null)
            _mutex.Close();
        }
        catch(Exception ex)
        {
          Debug.WriteLine(ex);
        }
        finally
        {
          _mutex = null;
        }
      }
    }
    

    这个例子展示了如何在程序中使用它。

    static class Program
    {
       static SingleInstance _myInstance = null;
    
       [STAThread]
       static void Main()
       {
         // ...
    
         try
         {
           // Create and keep instance reference until program exit
           _myInstance = new SingleInstance("MyUniqueProgramName");
    
           // By calling this property, this program instance requests ownership 
           // of the wrapped named mutex. The first program instance gets and keeps it 
           // until program exit. All other program instances cannot take mutex
           // ownership and exit here.
           if(_myInstance.IsRunning)
           {
             // You can show a message box, switch to the other program instance etc. here
    
             // Exit the program, another program instance is already running
             return;
           }
    
           // Run your app
    
         }
         finally
         {
           // Dispose the wrapper object and release mutex ownership, if owned
           _myInstance.Dispose();
         }
       }
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用此 sn-p 检查实例是否正在运行,并可以提醒用户另一个实例正在运行

         static bool IsRunning()
      {
          return Process.GetProcesses().Count(p => p.ProcessName.Contains(Assembly.GetExecutingAssembly().FullName.Split(',')[0]) && !p.Modules[0].FileName.Contains("vshost")) > 1;
      }
      

      【讨论】:

        猜你喜欢
        • 2011-04-12
        • 2013-02-13
        • 1970-01-01
        • 2014-02-11
        • 1970-01-01
        • 2018-11-11
        • 2018-03-12
        • 2012-01-19
        • 2014-12-18
        相关资源
        最近更新 更多