0

I have a test application which could not be loaded a several times. It works fine.

 private void App_OnStartup(object sender, StartupEventArgs e)
        {
    if (Process.GetProcessesByName(currentProcess.ProcessName).Length > 1)
                    {
                            MessageBox.Show("Instance already running", "Warning");
                            Application.Current.Shutdown();
                        }
    }
}

But what if a single user wants to create a shortcut of this application with another instance? For example "Test.exe" 2

enter image description here

This application should be loaded only once, when it was started like "Test.exe 2". I receive this number of instance in e.Args[0] and then I need to keep it for compare with another shortcuts of this application at this time.

So here's the question: how do I do it? How can I compare the already existing processes with a different instance's number. Any ideas appreciated!

Lab Lab
  • 781
  • 10
  • 34
  • @ASh Is there another way instead of using mutex, how do you think? – Lab Lab Aug 16 '17 at 09:52
  • 1
    Why do you call it an "instance number"? It's just a command line argument. Anyway checking the process name also makes no sense, a user could copy your application and rename the executable. – CodeCaster Aug 16 '17 at 09:54

1 Answers1

1

You could use Mutex to prevent multiple instances from your app to run simultaneously regardless of the name or path of the executable:

Prevent multiple instances of a given app in .NET?

mm8
  • 163,881
  • 10
  • 57
  • 88