示例#1
0
        private void LaunchGrowtopia()
        {
            RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Growtopia");
            string      path        = (string)registryKey.GetValue("path", "null") + @"\Growtopia.exe";

            registryKey.Dispose();

            if (File.Exists(path))
            {
                // Suspend all Growtopia
                foreach (var procs in Process.GetProcessesByName("Growtopia"))
                {
                    SuspendProcess(procs.Id);
                }

                Thread.Sleep(100);

                // Close own mutex
                if (mutex != null)
                {
                    mutex.Close();
                }

                // Run Growtopia and wait to initialize
                Process proc = Process.Start(path);
                proc.WaitForInputIdle();
                proc.WaitForInputIdle();
                proc.WaitForInputIdle();

                Thread.Sleep(100);

                // Set own mutex value
                mutex = new Mutex(true, "Growtopia");

                Thread.Sleep(100);

                // Close Growtopia mutex
                foreach (HandleInfo hi in Handles.EnumProcessHandles(proc.Id))
                {
                    if (hi.Type == "Mutant" && hi.Name.EndsWith("Growtopia"))
                    {
                        Handles.CloseHandleEx(proc.Id, hi.Handle);
                    }
                }

                Thread.Sleep(100);

                // Resume all Growtopia
                foreach (var procs in Process.GetProcessesByName("Growtopia"))
                {
                    ResumeProcess(procs.Id);
                }

                // Add item to listview and sort
                SetWindowText(proc.MainWindowHandle, "Growtopia " + GetWindowNumber());
                listView1.Items.Add(proc.MainWindowTitle).Checked = true;
                SortList();

                // Detect process exit
                proc.EnableRaisingEvents = true;
                proc.Exited += (ss, ee) => Process_Exited(ss, ee, proc);
            }
            else
            {
                ShowMessage("Unable to locate Growtopia,\nPlease re-install.");
            }
        }