Question on shutting down another program and wating for it to exit.
-
Forgive my newbie question. I am trying to learn C#. I have utterly confused myself in trying to restart a server console. I do not have the server start on this yet but it locks up every time I restart and I am not real sure way. It shutsdown ok but then never makes the check if the process has exited. Can anyone give me a nudge in the right direction? Thanks
public void restartserver(string serverrs, string serverloc) { Process p = Process.GetCurrentProcess(); Process\[\] servername = Process.GetProcessesByName(serverrs); IntPtr serverHandle = FindWindow(null, serverloc); if (serverHandle == IntPtr.Zero) { if (serverrs == "arcemu-world") { btnWorldStatus.Text = "World Server is not running"; } else { btnLogonStatus.Text = "Logon Server is not running"; } } else { SetForegroundWindow(serverHandle); SendKeys.SendWait("Shutdown{ENTER}"); FreeConsole(); bool mytrue = true; while (mytrue) { if (p.HasExited) { mytrue = false; if (serverrs == "arcemu-world") { btnWorldStatus.Text = "Closing...."; } else { btnLogonStatus.Text = "Closing...."; } } } } }
-
Forgive my newbie question. I am trying to learn C#. I have utterly confused myself in trying to restart a server console. I do not have the server start on this yet but it locks up every time I restart and I am not real sure way. It shutsdown ok but then never makes the check if the process has exited. Can anyone give me a nudge in the right direction? Thanks
public void restartserver(string serverrs, string serverloc) { Process p = Process.GetCurrentProcess(); Process\[\] servername = Process.GetProcessesByName(serverrs); IntPtr serverHandle = FindWindow(null, serverloc); if (serverHandle == IntPtr.Zero) { if (serverrs == "arcemu-world") { btnWorldStatus.Text = "World Server is not running"; } else { btnLogonStatus.Text = "Logon Server is not running"; } } else { SetForegroundWindow(serverHandle); SendKeys.SendWait("Shutdown{ENTER}"); FreeConsole(); bool mytrue = true; while (mytrue) { if (p.HasExited) { mytrue = false; if (serverrs == "arcemu-world") { btnWorldStatus.Text = "Closing...."; } else { btnLogonStatus.Text = "Closing...."; } } } } }
-
Forgive my newbie question. I am trying to learn C#. I have utterly confused myself in trying to restart a server console. I do not have the server start on this yet but it locks up every time I restart and I am not real sure way. It shutsdown ok but then never makes the check if the process has exited. Can anyone give me a nudge in the right direction? Thanks
public void restartserver(string serverrs, string serverloc) { Process p = Process.GetCurrentProcess(); Process\[\] servername = Process.GetProcessesByName(serverrs); IntPtr serverHandle = FindWindow(null, serverloc); if (serverHandle == IntPtr.Zero) { if (serverrs == "arcemu-world") { btnWorldStatus.Text = "World Server is not running"; } else { btnLogonStatus.Text = "Logon Server is not running"; } } else { SetForegroundWindow(serverHandle); SendKeys.SendWait("Shutdown{ENTER}"); FreeConsole(); bool mytrue = true; while (mytrue) { if (p.HasExited) { mytrue = false; if (serverrs == "arcemu-world") { btnWorldStatus.Text = "Closing...."; } else { btnLogonStatus.Text = "Closing...."; } } } } }
-
Forgive my newbie question. I am trying to learn C#. I have utterly confused myself in trying to restart a server console. I do not have the server start on this yet but it locks up every time I restart and I am not real sure way. It shutsdown ok but then never makes the check if the process has exited. Can anyone give me a nudge in the right direction? Thanks
public void restartserver(string serverrs, string serverloc) { Process p = Process.GetCurrentProcess(); Process\[\] servername = Process.GetProcessesByName(serverrs); IntPtr serverHandle = FindWindow(null, serverloc); if (serverHandle == IntPtr.Zero) { if (serverrs == "arcemu-world") { btnWorldStatus.Text = "World Server is not running"; } else { btnLogonStatus.Text = "Logon Server is not running"; } } else { SetForegroundWindow(serverHandle); SendKeys.SendWait("Shutdown{ENTER}"); FreeConsole(); bool mytrue = true; while (mytrue) { if (p.HasExited) { mytrue = false; if (serverrs == "arcemu-world") { btnWorldStatus.Text = "Closing...."; } else { btnLogonStatus.Text = "Closing...."; } } } } }
1. You are checking if your current process has exited instead of checking if the process you are killing has exited or not. 2. You are not using the Process array you have created (in the code block posted). So why even retrieve it? Following code might help you out:
Process\[\] processList = Process.GetProcessesByName("proccessName"); MessageBox.Show("Killing processes..."); try { foreach (Process process in processList) { process.Kill(); process.WaitForExit(); } MessageBox.Show("Processes killed successfully."); } catch (Exception exception){ // Log exception here MessageBox.Show("Could not kill on of the processes."); }
You can also use the other overload of
WaitForExit
and provide a suitable time out. In case you use that, then, after the call to the method, check if the process has exited and display the appropriate method if process is still running."No matter how many fish in the sea; it will be so empty without me." - From song "Without me" by Eminem
-