简体   繁体   中英

launching multiple instances of same exe

I'm trying to run a desktop program written in C# . I have created multiple users on the same computer to access from Remote Desktop.

I can successfully launch multiple instances from the same user's perspective, but my aim is to allow multiple users executing multiple executables from their own perspective.

I have also created different folders for each executable on each user's desktop.

I could successfully launch an executable from first user's perspective. But cannot execute same executable from the second user's perspective and vice versa.

executable just stops working when executing for the second time. log is as follows:

<ProblemSignatures>

<EventType>CLR20r3</EventType>

<Parameter0>MyExecutable.exe</Parameter0>

<Parameter1>1.0.3.30</Parameter1>

<Parameter2>56e6a1d2</Parameter2>

<Parameter3>mscorlib</Parameter3>

<Parameter4>4.6.1055.0</Parameter4>

<Parameter5>563c0eac</Parameter5>

<Parameter6>157f</Parameter6>

<Parameter7>12e</Parameter7>

<Parameter8>System.UnauthorizedAccess</Parameter8>

</ProblemSignatures>

I think I have found an answer. Problem was that my code was using mutex to prevent launching another instance. It looked like this and worked as expected when launching from single user's perspective by giving a message that another instance cannot be created:

 private static string appGuid = "39E8A84A-A531-4399-9B55-B480CB1C9B1D";
        //test
        [STAThread]
        static void Main()
        {
            using (Mutex mutex = new Mutex(false, "Global\\" + appGuid))
            {
                bool CheckAnotherInstance = true;
                if (CheckAnotherInstance && !mutex.WaitOne(0, false))
                {
                    MessageBox.Show("Only one executable can be run at the same time");
                    return;
                }

                RunProgram();
            }
        }

But it didn't give expected message when launched again from second user's perspective even if CheckAnotherInstance variable was false.

I have reorginized code like below and now it is working as expected when CheckAnotherInstance is false. Code is like this:

    private static string appGuid = "39E8A84A-A531-4399-9B55-B480CB1C9B1D";
        //test
        [STAThread]
        static void Main()
        {
            bool CheckAnotherInstance = false;
            if (CheckAnotherInstance)
            {
                using (Mutex mutex = new Mutex(false, "Global\\" + appGuid))
                {
                    if (!mutex.WaitOne(0, false))
                    {
                        MessageBox.Show("Only one executable can be run at the same time");
                        return;
                    }
                    RunMainProgram();
                }
            }
            else
            {
                RunMainProgram();
            }
        }

        private static void RunMainProgram()
        {    
            RunProgram();
        }

You can prevent multiple instances being run by a single user but still allow multiple users to run a single instance each. This is achieved without too much extra effort.

The following snippet appends the current user name on to the name of the mutex you are acquiring.

string userName = System.Threading.Thread.CurrentPrincipal.Identity.Name;
using (Mutex mutex = new Mutex(false, "Global\\" + appGuid + userName))
{
    // do your things
}

You can also use the following code if you want to stop running your exe from multiple instances (independent of windows user)

Dim ProcessName() As Process = Process.GetProcessesByName("MyExe")
If ProcessName.Length > 1 Then
        Messagebox.show "Exe is already running on another instance"
        End
End If

Try to use threading? Are you familiar with it? https://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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