diff --git a/Torch.Server/TorchServer.cs b/Torch.Server/TorchServer.cs index 811cfaa..002c496 100644 --- a/Torch.Server/TorchServer.cs +++ b/Torch.Server/TorchServer.cs @@ -178,6 +178,17 @@ namespace Torch.Server { if (State == ServerState.Stopped) Log.Error("Server is already stopped"); + if (Thread.CurrentThread == GameThread) + new Thread(StopInternal) + { + Name = "Stopping Thread" + }.Start(); + else + StopInternal(); + } + + private void StopInternal() + { Log.Info("Stopping server."); base.Stop(); Log.Info("Server stopped."); @@ -202,17 +213,22 @@ namespace Torch.Server Log.Info("Ejected all players from server for restart."); } - Stop(); - // TODO clone this - var config = (TorchConfig)Config; - LogManager.Flush(); + new Thread(() => + { + StopInternal(); + var config = (TorchConfig)Config; + LogManager.Flush(); - string exe = Assembly.GetExecutingAssembly().Location.Replace("dll", "exe"); - config.WaitForPID = Environment.ProcessId.ToString(); - config.TempAutostart = true; - Process.Start(exe, config.ToString()); + string exe = Assembly.GetExecutingAssembly().Location.Replace("dll", "exe"); + config.WaitForPID = Environment.ProcessId.ToString(); + config.TempAutostart = true; + Process.Start(exe, config.ToString()); - Environment.Exit(0); + Environment.Exit(0); + }) + { + Name = "Restart thread" + }.Start(); } [SuppressPropertyChangedWarnings] diff --git a/Torch/VRageGame.cs b/Torch/VRageGame.cs index af828d7..017edd5 100644 --- a/Torch/VRageGame.cs +++ b/Torch/VRageGame.cs @@ -472,7 +472,8 @@ namespace Torch { // Kinda icky, but we can't block the update and expect the state to change. if (Thread.CurrentThread == _updateThread) - return _state == state; + throw new InvalidOperationException( + "Waiting for game state is not possible from update thread (deadlock)"); DateTime? end = timeout.HasValue ? (DateTime?) (DateTime.Now + timeout.Value) : null; while (_state != state && (!end.HasValue || end > DateTime.Now + TimeSpan.FromSeconds(1)))