Console shutdown mechanism
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
I'm trying to create an effiecent way to shut down my console application. To do this I have a while loop in my program's main method waiting for a request to shutdown.
var rebootAfterClose = false;
while (true)
Server?.Stop(rebootAfterClose);
I then have these methods, the stop is the one that gets called, Dispose gets called in Stop().
public void Dispose()
if (Disposing)
return;
Disposing = true;
BaseHandler.PlayerHandler.Dispose();
ConsoleUpdater.Dispose();
SocketHandler.Dispose();
public void Stop(bool restart)
Task.Factory.StartNew(() =>
var shutdownMessage = PlusEnvironment.GetLanguageManager().TryGetValue("server.shutdown.message");
var shutdownPacket = new BroadcastMessageAlertComposer(shutdownMessage);
Program.Server.BaseHandler.PlayerHandler.SendPacketToPlayers(shutdownPacket);
ConsoleUpdater.Stop();
Console.Clear();
Console.WriteLine();
Console.Title = "Stopping...";
Logger.Warn("Server is " + (restart ? "rebooting" : "shutting down") + "....");
Thread.Sleep(5000);
// not sure if I should use this instead? : Task.Delay(5000);
Dispose();
if (restart)
Process.Start(Assembly.GetExecutingAssembly().Location);
Environment.Exit(0);
);
I used a thread as I didn't want to block the thread with the 5 second delay. I'm currently looking to just tidy this mechanism up as I am really new to multithreading and just want to know if I did okay, I appreciate everyone who contributes a comment or answer.
Another thing I'm worried about is the while (true) loop in my main method, is there any better way to do this? I need to avoid closing the console application on any other key other than the ones in the while loop (CTR+C], or CTRL+R) but I can't think of a better way to do that.
c# multithreading .net console
add a comment |Â
up vote
2
down vote
favorite
I'm trying to create an effiecent way to shut down my console application. To do this I have a while loop in my program's main method waiting for a request to shutdown.
var rebootAfterClose = false;
while (true)
Server?.Stop(rebootAfterClose);
I then have these methods, the stop is the one that gets called, Dispose gets called in Stop().
public void Dispose()
if (Disposing)
return;
Disposing = true;
BaseHandler.PlayerHandler.Dispose();
ConsoleUpdater.Dispose();
SocketHandler.Dispose();
public void Stop(bool restart)
Task.Factory.StartNew(() =>
var shutdownMessage = PlusEnvironment.GetLanguageManager().TryGetValue("server.shutdown.message");
var shutdownPacket = new BroadcastMessageAlertComposer(shutdownMessage);
Program.Server.BaseHandler.PlayerHandler.SendPacketToPlayers(shutdownPacket);
ConsoleUpdater.Stop();
Console.Clear();
Console.WriteLine();
Console.Title = "Stopping...";
Logger.Warn("Server is " + (restart ? "rebooting" : "shutting down") + "....");
Thread.Sleep(5000);
// not sure if I should use this instead? : Task.Delay(5000);
Dispose();
if (restart)
Process.Start(Assembly.GetExecutingAssembly().Location);
Environment.Exit(0);
);
I used a thread as I didn't want to block the thread with the 5 second delay. I'm currently looking to just tidy this mechanism up as I am really new to multithreading and just want to know if I did okay, I appreciate everyone who contributes a comment or answer.
Another thing I'm worried about is the while (true) loop in my main method, is there any better way to do this? I need to avoid closing the console application on any other key other than the ones in the while loop (CTR+C], or CTRL+R) but I can't think of a better way to do that.
c# multithreading .net console
2
I'd be better if you posted the completeProgram
class rather then just snippets or whatever your class is called.
â t3chb0t
Jan 26 at 18:33
There is literally 1 lineServer = new Server();
and that's the rest of it, I would of posted it if it was relevant in any way.
â ropuxil
Jan 26 at 20:44
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm trying to create an effiecent way to shut down my console application. To do this I have a while loop in my program's main method waiting for a request to shutdown.
var rebootAfterClose = false;
while (true)
Server?.Stop(rebootAfterClose);
I then have these methods, the stop is the one that gets called, Dispose gets called in Stop().
public void Dispose()
if (Disposing)
return;
Disposing = true;
BaseHandler.PlayerHandler.Dispose();
ConsoleUpdater.Dispose();
SocketHandler.Dispose();
public void Stop(bool restart)
Task.Factory.StartNew(() =>
var shutdownMessage = PlusEnvironment.GetLanguageManager().TryGetValue("server.shutdown.message");
var shutdownPacket = new BroadcastMessageAlertComposer(shutdownMessage);
Program.Server.BaseHandler.PlayerHandler.SendPacketToPlayers(shutdownPacket);
ConsoleUpdater.Stop();
Console.Clear();
Console.WriteLine();
Console.Title = "Stopping...";
Logger.Warn("Server is " + (restart ? "rebooting" : "shutting down") + "....");
Thread.Sleep(5000);
// not sure if I should use this instead? : Task.Delay(5000);
Dispose();
if (restart)
Process.Start(Assembly.GetExecutingAssembly().Location);
Environment.Exit(0);
);
I used a thread as I didn't want to block the thread with the 5 second delay. I'm currently looking to just tidy this mechanism up as I am really new to multithreading and just want to know if I did okay, I appreciate everyone who contributes a comment or answer.
Another thing I'm worried about is the while (true) loop in my main method, is there any better way to do this? I need to avoid closing the console application on any other key other than the ones in the while loop (CTR+C], or CTRL+R) but I can't think of a better way to do that.
c# multithreading .net console
I'm trying to create an effiecent way to shut down my console application. To do this I have a while loop in my program's main method waiting for a request to shutdown.
var rebootAfterClose = false;
while (true)
Server?.Stop(rebootAfterClose);
I then have these methods, the stop is the one that gets called, Dispose gets called in Stop().
public void Dispose()
if (Disposing)
return;
Disposing = true;
BaseHandler.PlayerHandler.Dispose();
ConsoleUpdater.Dispose();
SocketHandler.Dispose();
public void Stop(bool restart)
Task.Factory.StartNew(() =>
var shutdownMessage = PlusEnvironment.GetLanguageManager().TryGetValue("server.shutdown.message");
var shutdownPacket = new BroadcastMessageAlertComposer(shutdownMessage);
Program.Server.BaseHandler.PlayerHandler.SendPacketToPlayers(shutdownPacket);
ConsoleUpdater.Stop();
Console.Clear();
Console.WriteLine();
Console.Title = "Stopping...";
Logger.Warn("Server is " + (restart ? "rebooting" : "shutting down") + "....");
Thread.Sleep(5000);
// not sure if I should use this instead? : Task.Delay(5000);
Dispose();
if (restart)
Process.Start(Assembly.GetExecutingAssembly().Location);
Environment.Exit(0);
);
I used a thread as I didn't want to block the thread with the 5 second delay. I'm currently looking to just tidy this mechanism up as I am really new to multithreading and just want to know if I did okay, I appreciate everyone who contributes a comment or answer.
Another thing I'm worried about is the while (true) loop in my main method, is there any better way to do this? I need to avoid closing the console application on any other key other than the ones in the while loop (CTR+C], or CTRL+R) but I can't think of a better way to do that.
c# multithreading .net console
edited Jan 26 at 18:30
t3chb0t
32.1k54195
32.1k54195
asked Jan 26 at 18:07
ropuxil
343
343
2
I'd be better if you posted the completeProgram
class rather then just snippets or whatever your class is called.
â t3chb0t
Jan 26 at 18:33
There is literally 1 lineServer = new Server();
and that's the rest of it, I would of posted it if it was relevant in any way.
â ropuxil
Jan 26 at 20:44
add a comment |Â
2
I'd be better if you posted the completeProgram
class rather then just snippets or whatever your class is called.
â t3chb0t
Jan 26 at 18:33
There is literally 1 lineServer = new Server();
and that's the rest of it, I would of posted it if it was relevant in any way.
â ropuxil
Jan 26 at 20:44
2
2
I'd be better if you posted the complete
Program
class rather then just snippets or whatever your class is called.â t3chb0t
Jan 26 at 18:33
I'd be better if you posted the complete
Program
class rather then just snippets or whatever your class is called.â t3chb0t
Jan 26 at 18:33
There is literally 1 line
Server = new Server();
and that's the rest of it, I would of posted it if it was relevant in any way.â ropuxil
Jan 26 at 20:44
There is literally 1 line
Server = new Server();
and that's the rest of it, I would of posted it if it was relevant in any way.â ropuxil
Jan 26 at 20:44
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186070%2fconsole-shutdown-mechanism%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
I'd be better if you posted the complete
Program
class rather then just snippets or whatever your class is called.â t3chb0t
Jan 26 at 18:33
There is literally 1 line
Server = new Server();
and that's the rest of it, I would of posted it if it was relevant in any way.â ropuxil
Jan 26 at 20:44