我希望在处理时为我的服务器添加延迟,以便用户可以实际读取关闭消息,而我无法通过线程休眠来执行此操作,因为它会冻结整个游戏。
我决定改变我的功能以使用异步任务,但它似乎只是立即关闭,甚至没有处理哪个是真正的功能杀死。
首先,我检查一个键输入,告诉我关机或重启......
var rebootAfterClose = false;
while (true)
{
var input = Console.ReadKey();
if (input.Key == ConsoleKey.C && input.Modifiers == ConsoleModifiers.Control)
{
break;
}
if (input.Key != ConsoleKey.R || input.Modifiers != ConsoleModifiers.Control)
{
continue;
}
rebootAfterClose = true;
break;
}
Server?.Stop(rebootAfterClose);
然后我有了实际关闭的代码
public async Task Stop(bool restart)
{
var shutdownMessage = PlusEnvironment.GetLanguageManager().TryGetValue("server.shutdown.message");
var shutdownPacket = new BroadcastMessageAlertComposer(shutdownMessage);
Program.Server.BaseHandler.PlayerHandler.SendPacketToPlayers(shutdownPacket);
await Task.Delay(5000);
// TODO: Start the process if restart = true
Dispose();
}
public void Dispose()
{
if (Disposing)
{
return;
}
Disposing = true;
Console.Clear();
Console.WriteLine();
Console.Title = "Server is shutting down...";
Logger.Warn("Server is shutting down...");
BaseHandler.PlayerHandler.Dispose();
ConsoleUpdater.Dispose();
SocketHandler.Dispose();
PlusEnvironment.OnShutDown();
Thread.Sleep(2000);
Environment.Exit(0);
}