0

I developed a single instance WinForm application using .net 4.5 and a mutex on the Main class. Some user report that they can't start the application because the mutex is already taken.

    static string guid = ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), true)[0]).Value + "-OrientalWave";
    public static string GUID { get { return guid; } }

    [STAThread]
    static void Main(string[] args)
    {
        AppDomain currentDomain = AppDomain.CurrentDomain;
        currentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionHandler);

        // Single Instance Check
        bool createdNew;
        using (var mutex = new Mutex(true, guid, out createdNew))
        {
            if (createdNew)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                if (args.Length > 0)
                {
                    string filename = args[0];
                    Application.Run(new frmMain(filename));
                }
                else
                    Application.Run(new frmMain());
            }
            else
            {
                if (args.Length > 0)
                {
                    // If i want to open a file
                    string filename = args[0];

                    NamedPipesClient pipeClient = new NamedPipesClient();
                    pipeClient.Start();
                    pipeClient.SendMessage(filename);
                    pipeClient.Stop();
                }
                else
                    MessageBox.Show("Only single instance allowed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }

    static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs args)
    {
        try
        {
            var exception = (Exception)args.ExceptionObject;
            MessageBox.Show(exception.Message, null, MessageBoxButtons.OK, MessageBoxIcon.Error);
            Logger.Write(Logger.LogType.Error, exception.Source, "Message: " + exception.Message + " - Stack: " + exception.StackTrace);
        }
        catch { }
        Environment.Exit(0);
    }

I searched the web for a solution or an alternative way to create a single instance application but with no success.

  • 1
    You made that a feature of your app, so not terribly surprising. Do avoid re-inventing this wheel, it is already supported by the framework. Always favor code that has been slammed by hundreds of thousand of programmers, being tested millions of times every day. https://stackoverflow.com/a/29260770/17034 – Hans Passant Dec 16 '17 at 17:22
  • In my opinion this code should work as expected... – Legends Dec 18 '17 at 00:35

1 Answers1

0

It can happen if there is any abandoned mutex. You should catch AbandonedMutexException and release the mutex by calling ReleaseMutex(); Then you should be able to acquire a mutex.

You can find a similar use case here http://gonetdotnet.blogspot.in/2017/08/solved-how-to-handle-log4net.html

Libish Jacob
  • 308
  • 4
  • 8