简体   繁体   中英

Wait to finish before doing the next task

i have a server application with a richtextbox as the console text basically. The problem is, i have start, restart and stop buttons - But my restart button doesn't work.

Heres my code for the restart button:

consoletxt("RESTART", "Restarting Server...");
statuslabel1.Text = "Restarting";
statuslabel1.ForeColor = Color.Orange;
statuslabel2.Text = "Restarting";
statuslabel2.ForeColor = Color.Orange;

command("stop");

//The performclick just starts the server
startbtn.PerformClick();

statuslabel1.Text = "Online";
statuslabel1.ForeColor = Color.DarkGreen;
statuslabel2.Text = "Online";
statuslabel2.ForeColor = Color.DarkGreen;
consoletxt("RESTART", "Restart completed, server online!");

However, the output is this:

2012-04-01 11:32:12 [RESTART] Restarting Server...
2012-04-01 11:32:12 [RESTART] Restart completed, server online!
2012-04-01 11:32:12 [INFO] CONSOLE: Stopping the server..
2012-04-01 11:32:12 [INFO] Stopping server

So, it says that the restart has finished in the text - but it hasn't - all it has done is stop the server.

CODE FOR PERFORMCLICK:

 try { var x = ServerProc.StartTime; return; }
            catch { }
            try
            {
                ServerProc.Start();
                ServerProc.BeginErrorReadLine();
                statuslabel1.Text = "Online";
                statuslabel1.ForeColor = Color.DarkGreen;
                statuslabel2.Text = "Online";
                statuslabel2.ForeColor = Color.DarkGreen;

            }

Please can sombody explain to me how to do this properly?

Thanks!

Okay, well:

Firstly you should never just catch an exception and do nothing. Presumably you're expecting a certainly kind of exception if the server hasn't already been started - so catch that specific exception, and ideally log it somewhere. Alternatively, if at all possible, create some more appropriate API for detecting whether it's already started.

Secondly, assuming that ServerProc is a Process , you've started the process itself - but that doesn't mean the server is "up and running and ready to serve" at the moment. It sounds like you probably want to some sort of polling so that you can tell when the server is actually running (or has failed to start properly). It's hard to know what that should look like without knowing what your server is meant to do. You could potentially make it write to its standard output/error when it's actually ready, or you could make some harmless request to it until it does respond - presumably with a timeout so that if it takes more than a few seconds to start, you can abort.

EDIT: It sounds like you probably want to move the final bit of code which says "Online" (which appears to be in two places...) to a separate method. When your "line handling" code spots the "I've started" message it should then report the server as having started. You might additionally want to set a timer such that if the server hasn't started within a couple of minutes, you abort the process or whatever is appropriate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM