繁体   English   中英

是否使用互斥锁来防止同一程序的多个实例安全运行?

[英]Is using a Mutex to prevent multiple instances of the same program from running safe?

我正在使用此代码来防止我的程序的第二个实例同时运行,是否安全?

Mutex appSingleton = new System.Threading.Mutex(false, "MyAppSingleInstnceMutx");
if (appSingleton.WaitOne(0, false)) {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new MainForm());
    appSingleton.Close();
} else {
    MessageBox.Show("Sorry, only one instance of MyApp is allowed.");
}

我担心如果出现异常并且应用程序崩溃,互斥锁仍将被保留。 真的吗?

为此目的使用 Windows 事件更为常见和方便。 例如

static EventWaitHandle s_event ;

bool created ;
s_event = new EventWaitHandle (false, 
    EventResetMode.ManualReset, "my program#startup", out created) ;
if (created) Launch () ;
else         Exit   () ;

当您的进程退出或终止时,Windows 将为您关闭该事件,如果没有打开的句柄,则将其销毁。

添加:要管理会话,请使用Local\\Global\\前缀作为事件(或互斥锁)名称。 如果您的应用程序是每个用户的,只需将适当修改的登录用户名附加到事件名称。

一般来说是的,这会起作用。 然而,魔鬼在细节中。

首先,您要在finally块中关闭互斥锁。 否则,您的进程可能会突然终止并使其处于有信号状态,例如异常。 这将使未来的流程实例无法启动。

不幸的是,即使有finally块,您也必须处理一个进程将在不释放互斥锁的情况下终止的可能性。 例如,如果用户通过 TaskManager 终止进程,就会发生这种情况。 您的代码中存在竞争条件,允许第二个进程在WaitOne调用中获得AbandonedMutexException 为此,您需要一个恢复策略。

我鼓励您阅读Mutex 类的详细信息 使用它并不总是那么简单。


扩展竞争条件的可能性:

以下事件序列可能会导致应用程序的第二个实例抛出:

  1. 正常进程启动。
  2. 第二个进程启动并获取互斥锁的句柄,但在WaitOne调用之前被关闭。
  3. 进程#1 突然终止。 互斥体不会被销毁,因为进程 #2 有一个句柄。 相反,它被设置为废弃状态。
  4. 第二个进程再次开始运行并得到一个AbanonedMutexException

您可以使用互斥锁,但首先要确保这确实是您想要的。

因为“避免多个实例”没有明确定义。 这可能意味着

  1. 避免在同一个用户会话中启动多个实例,无论该用户会话有多少个桌面,而是允许多个实例为不同的用户会话同时运行。
  2. 避免在同一个桌面中启动多个实例,但允许多个实例运行,只要每个实例都在单独的桌面中。
  3. 避免为同一个用户帐户启动多个实例,无论存在多少个在此帐户下运行的桌面或会话,但允许多个实例为在不同用户帐户下运行的会话同时运行。
  4. 避免在同一台机器上启动多个实例。 这意味着无论任意数量的用户使用多少个桌面,最多只能运行一个程序实例。

通过使用互斥锁,您基本上是在使用定义数字 4。

我使用这种方法,我相信它是安全的,因为如果任何应用程序不再持有互斥锁,就会销毁互斥锁​​(如果最初无法创建互斥锁,应用程序将被终止)。 这在“AppDomain-processes”中可能会或可能不会完全相同(请参阅底部的链接):

// Make sure that appMutex has the lifetime of the code to guard --
// you must keep it from being collected (and the finalizer called, which
// will release the mutex, which is not good here!).
// You can also poke the mutex later.
Mutex appMutex;

// In some startup/initialization code
bool createdNew;
appMutex = new Mutex(true, "mutexname", out createdNew);
if (!createdNew) {
  // The mutex already existed - exit application.
  // Windows will release the resources for the process and the
  // mutex will go away when no process has it open.
  // Processes are much more cleaned-up after than threads :)
} else {
  // win \o/
}

以上受到其他答案/评论中关于恶意程序能够坐在互斥锁上的注释的影响。 这里不关心。 此外,在“本地”空间中创建了无前缀的互斥锁。 这可能是正确的。

请参阅: http : //ayende.com/Blog/archive/2008/02/28/The-mysterious-life-of-mutexes.aspx——与 Jon Skeet 一起提供;-)

在 Windows 上,终止进程会产生以下结果:

  • 进程中的任何剩余线程都被标记为终止。
  • 进程分配的任何资源都将被释放。
  • 所有内核对象都已关闭。
  • 进程代码从内存中删除。
  • 进程退出代码已设置。
  • 进程对象被通知。

互斥对象是内核对象,因此当进程终止时(无论如何在 Windows 中),进程持有的任何对象都将关闭。

但是,请注意 CreateMutex() 文档中的以下内容:

如果您使用命名互斥锁将您的应用程序限制为单个实例,恶意用户可以在您之前创建此互斥锁并阻止您的应用程序启动。

这是代码片段

public enum ApplicationSingleInstanceMode
{
    CurrentUserSession,
    AllSessionsOfCurrentUser,
    Pc
}

public class ApplicationSingleInstancePerUser: IDisposable
{
    private readonly EventWaitHandle _event;

    /// <summary>
    /// Shows if the current instance of ghost is the first
    /// </summary>
    public bool FirstInstance { get; private set; }

    /// <summary>
    /// Initializes 
    /// </summary>
    /// <param name="applicationName">The application name</param>
    /// <param name="mode">The single mode</param>
    public ApplicationSingleInstancePerUser(string applicationName, ApplicationSingleInstanceMode mode = ApplicationSingleInstanceMode.CurrentUserSession)
    {
        string name;
        if (mode == ApplicationSingleInstanceMode.CurrentUserSession)
            name = $"Local\\{applicationName}";
        else if (mode == ApplicationSingleInstanceMode.AllSessionsOfCurrentUser)
            name = $"Global\\{applicationName}{Environment.UserDomainName}";
        else
            name = $"Global\\{applicationName}";

        try
        {
            bool created;
            _event = new EventWaitHandle(false, EventResetMode.ManualReset, name, out created);
            FirstInstance = created;
        }
        catch
        {
        }
    }

    public void Dispose()
    {
        _event.Dispose();
    }
}

是的,这是安全的,我建议采用以下模式,因为您需要确保始终释放Mutex

using( Mutex mutex = new Mutex( false, "mutex name" ) )
{
    if( !mutex.WaitOne( 0, true ) )
    {
        MessageBox.Show("Unable to run multiple instances of this program.",
                        "Error",  
                        MessageBoxButtons.OK, 
                        MessageBoxIcon.Error);
    }
    else
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainForm());                  
    }
}

这是我如何处理这个

在程序类中:

  1. 使用 Process.GetCurrentProcess() 获取您的应用程序的 System.Diagnostics.Process

  2. 使用 Process.GetProcessesByName(thisProcess.ProcessName) 使用应用程序的当前名称逐步浏览打开的进程集合

  3. 根据 thisProcess.Id 检查每个 process.Id,如果一个实例已经打开,那么至少有 1 个将匹配名称但不匹配 Id,否则继续打开实例

    using System.Diagnostics; ..... static void Main() { Process thisProcess = Process.GetCurrentProcess(); foreach(Process p in Process.GetProcessesByName(thisProcess.ProcessName)) { if(p.Id != thisProcess.Id) { // Do whatever u want here to alert user to multiple instance return; } } // Continue on with opening application

完成此操作的一个很好的方法是向用户展示已经打开的实例,很可能他们不知道它是打开的,所以让我们向他们展示它是。 为此,我使用 User32.dll 将一条消息广播到 Windows 消息传递循环中,一条自定义消息,并且我让我的应用程序在 WndProc 方法中侦听它,如果它收到此消息,它会将自己呈现给用户 Form .Show() 或诸如此类。

使用带有超时和安全设置的应用程序,避免 AbandonedMutexException。 我使用了我的自定义类:

private class SingleAppMutexControl : IDisposable
    {
        private readonly Mutex _mutex;
        private readonly bool _hasHandle;

        public SingleAppMutexControl(string appGuid, int waitmillisecondsTimeout = 5000)
        {
            bool createdNew;
            var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null),
                MutexRights.FullControl, AccessControlType.Allow);
            var securitySettings = new MutexSecurity();
            securitySettings.AddAccessRule(allowEveryoneRule);
            _mutex = new Mutex(false, "Global\\" + appGuid, out createdNew, securitySettings);
            _hasHandle = false;
            try
            {
                _hasHandle = _mutex.WaitOne(waitmillisecondsTimeout, false);
                if (_hasHandle == false)
                    throw new System.TimeoutException();
            }
            catch (AbandonedMutexException)
            {
                _hasHandle = true;
            }
        }

        public void Dispose()
        {
            if (_mutex != null)
            {
                if (_hasHandle)
                    _mutex.ReleaseMutex();
                _mutex.Dispose();
            }
        }
    }

并使用它:

    private static void Main(string[] args)
    {
        try
        {
            const string appguid = "{xxxxxxxx-xxxxxxxx}";
            using (new SingleAppMutexControl(appguid))
            {
                //run main app
                Console.ReadLine();
            }
        }
        catch (System.TimeoutException)
        {
            Log.Warn("Application already runned");
        }
        catch (Exception ex)
        {
            Log.Fatal(ex, "Fatal Error on running");
        }
    }

重要的是要记住,这个互斥体\\事件可以通过 Microsoft sysinternals 的 handle.exe 之类的程序轻松查看,然后如果使用常量名称,邪恶的人可能会使用它来阻止您的应用程序启动。

以下是微软关于安全替代方案的一些建议:

但是,恶意用户可以在您之前创建此互斥锁并阻止您的应用程序启动。 为防止出现这种情况,请创建一个随机命名的互斥锁并存储该名称,以便只能由授权用户获取。 或者,您可以为此目的使用文件。 要将您的应用程序限制为每个用户一个实例,请在用户的配置文件目录中创建一个锁定文件。

取自此处: https : //docs.microsoft.com/en-us/sysinternals/downloads/handle

如果您想使用基于互斥锁的方法,您应该真正使用本地互斥锁来限制该方法仅限于当前用户的登录会话 还要注意该链接中关于使用互斥锁方法进行稳健资源处理的另一个重要警告。

一个警告是,当用户尝试启动第二个实例时,基于互斥锁的方法不允许您激活应用程序的第一个实例。

另一种方法是在第一个实例上 PInvoke 到 FindWindow,然后是 SetForegroundWindow。 另一种选择是按名称检查您的进程:

Process[] processes = Process.GetProcessesByName("MyApp");
if (processes.Length != 1)
{
    return;
} 

后两种选择都有一个假设的竞争条件,即应用程序的两个实例可以同时启动,然后相互检测。 这在实践中不太可能发生 - 事实上,在测试期间我无法实现。

后两个替代方案的另一个问题是它们在使用终端服务时不起作用。

暂无
暂无

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

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