void ErrorListener() { while (true) { try { string line = process.StandardError.ReadLine(); if (line == null) { return; } BackendManager.SendToClients(Name + "\terror\t" + line); LastReceived = DateTime.Now; if (line.Contains(" [SEVERE] Unexpected exception")) { //restart Log("Sending stop because: " + line); SendCommand("stop"); } } catch (Exception e) { Log(e); } } }
void OutListener() { while (true) { try { string line = process.StandardOutput.ReadLine(); if (line == null) { return; } LastReceived = DateTime.Now; BackendManager.SendToClients(Name + "\tout\t" + line); } catch (Exception e) { Log(e); } } }
public void Run() { Log("run"); while (Program.Exit.WaitOne(0) == false) { if (exit) { Log("exit loop"); break; } Log("Starting: " + psi.FileName + " " + psi.Arguments); using (Process p = Process.Start(psi)) { try { Running.Set(); BackendManager.SendToClients(Name + "\tstarted"); Console.Error.WriteLine("Minecraft server running"); lock (processLock) { process = p; } Log("started"); Thread olt = new Thread(OutListener); Thread elt = new Thread(ErrorListener); olt.Start(); elt.Start(); p.WaitForExit(); Log("Exited with code: " + p.ExitCode); olt.Abort(); elt.Abort(); } catch (Exception e) { Log(e); } finally { lock (processLock) { Kill(); process = null; } Running.Reset(); try { BackendManager.SendToClients(Name + "\tstopped"); } catch (Exception ex) { Log(ex); } } Console.Error.WriteLine("Restarting minecraft"); } } shutdown.Set(); }