C#方法中的互斥体初始化总是返回正createdNew

C#方法中的互斥体初始化总是返回正createdNew,c#,wpf,mutex,single-instance,C#,Wpf,Mutex,Single Instance,我创建了简单的互斥管理器: public static class MutexManager { private static string mutexName { get { return "MyAppName" + System.Security.Principal.WindowsIdentity.GetCurrent().User.AccountDomainSid; } } publi

我创建了简单的互斥管理器

public static class MutexManager
{
    private static string mutexName
    {
        get
        {
            return "MyAppName" + System.Security.Principal.WindowsIdentity.GetCurrent().User.AccountDomainSid;
        }
    }

    public static bool CreateApplicationMutex()
    {

        bool createdNew;
        var mutex = new Mutex(false, mutexName, out createdNew);                       

        return createdNew;
    }
}

问题是,CreateApplicationMutex总是在新应用程序实例启动时返回true。只要我在app.cs中有完全相同的代码,一切都是正确的,但在我将其移动到MutexManagercreatedNew之后,总是正确的。我做错了什么?

以下代码对我来说工作正常,第二次返回false

public static class MutexManager
{
   private static string mutexName => "MyAppName" + System.Security.Principal.WindowsIdentity.GetCurrent()
                                                            .User?.AccountDomainSid;
   public static bool CreateApplicationMutex()
   {
      new Mutex(false, mutexName, out var createdNew);

      return createdNew;
   }
}

private static void Main(string[] args)
{
   Console.WriteLine(MutexManager.CreateApplicationMutex());
   Console.ReadKey();
}
输出

true
false

确保您调试了应用程序,并检查互斥体名称

更新 Winforms

MessageBox.Show(
   MutexManager.CreateApplicationMutex()
               .ToString());
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
WPF

public partial class App : Application
{
   protected override void OnStartup(StartupEventArgs e)
   {
      MessageBox.Show(
         MutexManager.CreateApplicationMutex()
                     .ToString());
      base.OnStartup(e);
   }
}

它再次按预期工作,无法复制

我也有类似的问题,原因是互斥变量的生存时间。以下代码在调试版本中运行良好,但对于发布版本中创建的新代码,始终返回true

using System;

static class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        string mutex_name = "MyApplicationName_SingleInstanceMutex";
        bool created_new = false;
        System.Threading.Mutex mutex = new System.Threading.Mutex(false, mutex_name, out created_new);
        if (!created_new)
        {
            System.Windows.Forms.MessageBox.Show("Application is already started!");
            return;
        }
        
        /* Create & run mainForm here */
    }
}
设置互斥静态可解决此问题:

using System;

static class Program
{
    static System.Threading.Mutex mutex = null;

    [STAThread]
    static void Main(string[] args)
    {
        string mutex_name = "MyApplicationName_SingleInstanceMutex";
        bool created_new = false;
        mutex = new System.Threading.Mutex(false, mutex_name, out created_new);
        if (!created_new)
        {
            System.Windows.Forms.MessageBox.Show("Application is already started!");
            return;
        }
        
        /* Create & run mainForm here */
    }
}

它在控制台应用程序中看起来工作得很好,但当我在ApplicationStart(我的WPF应用程序启动点)中调用此方法时,相同的代码并不像预期的那样工作。如果我将这段代码从MutexManager移到ApplicationStart,它是可以工作的,但我希望将它保留在那里。@BartekChyży在winforms和wpf中进行了测试,并且它在所有3个组件之间以任何组合工作,并且无法重现您的结果谢谢您的时间。如果我找到了解决方案,我会通知您。我必须将其包装在dispatcher调用中,但我不完全理解为什么。如果mutexName在两个实例中返回相同的值,则应该可以工作。确保以同一用户的身份启动应用程序。