forced stop & restart in separate thread

This commit is contained in:
z__
2022-02-12 01:42:46 +07:00
parent ce2bbd4a61
commit d138a46f25
2 changed files with 27 additions and 10 deletions

View File

@@ -178,6 +178,17 @@ namespace Torch.Server
{ {
if (State == ServerState.Stopped) if (State == ServerState.Stopped)
Log.Error("Server is already 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."); Log.Info("Stopping server.");
base.Stop(); base.Stop();
Log.Info("Server stopped."); Log.Info("Server stopped.");
@@ -202,8 +213,9 @@ namespace Torch.Server
Log.Info("Ejected all players from server for restart."); Log.Info("Ejected all players from server for restart.");
} }
Stop(); new Thread(() =>
// TODO clone this {
StopInternal();
var config = (TorchConfig)Config; var config = (TorchConfig)Config;
LogManager.Flush(); LogManager.Flush();
@@ -213,6 +225,10 @@ namespace Torch.Server
Process.Start(exe, config.ToString()); Process.Start(exe, config.ToString());
Environment.Exit(0); Environment.Exit(0);
})
{
Name = "Restart thread"
}.Start();
} }
[SuppressPropertyChangedWarnings] [SuppressPropertyChangedWarnings]

View File

@@ -472,7 +472,8 @@ namespace Torch
{ {
// Kinda icky, but we can't block the update and expect the state to change. // Kinda icky, but we can't block the update and expect the state to change.
if (Thread.CurrentThread == _updateThread) 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; DateTime? end = timeout.HasValue ? (DateTime?) (DateTime.Now + timeout.Value) : null;
while (_state != state && (!end.HasValue || end > DateTime.Now + TimeSpan.FromSeconds(1))) while (_state != state && (!end.HasValue || end > DateTime.Now + TimeSpan.FromSeconds(1)))