Console shutdown mechanism

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite
1












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.







share|improve this question

















  • 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
















up vote
2
down vote

favorite
1












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.







share|improve this question

















  • 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












up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





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.







share|improve this question













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.









share|improve this question












share|improve this question




share|improve this question








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 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












  • 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







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















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?