c# - Waiting for commands to be complete -


i working winform runs cmd in background, redirecting input , output asynchronously.

currently, winform iterating through array of commands, writing each cmd via streamwriter standardinput redirected to. how can force loop wait until present command complete in cmd before writing next line in?

edit: took out of actual project code, , replaced this, stripped down version of i'm trying do, including components of project relevant question.

public partial class form1 : form {      public delegate void writetoconsolemethod(string text);     process _process;      string[] _commands =      {         "echo hello world",         "echo name t.k.",         "echo here list of commands"     };     public form1()     {         initializecomponent();         processstartinfo processstartinfo = new processstartinfo("cmd")         {             redirectstandarderror = true,             redirectstandardinput = true,             redirectstandardoutput = true,             useshellexecute = false,             createnowindow = true         };          _process = process.start(processstartinfo);         _process.outputdatareceived += new datareceivedeventhandler(new datareceivedeventhandler(datareceived_eventhandler));         _process.errordatareceived += new datareceivedeventhandler(new datareceivedeventhandler(datareceived_eventhandler));         _process.beginerrorreadline();         _process.beginoutputreadline();     }      private void datareceived_eventhandler(object sender, datareceivedeventargs e)     {         iasyncresult result = this.begininvoke(new writetoconsolemethod(writetoconsole), new object[] { e.data + environment.newline });         this.endinvoke(result);     }      private void writetoconsole(string output)     {         txtbxconsole.appendtext(output);     }      private void btnbegin_click(object sender, eventargs e)     {         foreach (string command in _commands)         {             _process.standardinput.writeline(command);             // want way pause here until cmd has finished processing command.         }     } } 

i don't think there built-in support that. send own special command , wait until see in output example , :

    const string separator= "---command completed--\xe3\xe2\xe1\xe0\xe3";        // has won't occur in normal output.        volatile bool finished = false;      private void button1_click(object sender, eventargs e)     {                  foreach (string command in _commands)             run(command);     }      private void writetoconsole(string output)     {         if (output.indexof(separator) >= 0)             finished = true;         else             richtextbox1.appendtext(output);     }       private void run(string command)     {         finished = false;         _process.standardinput.writeline(command);         _process.standardinput.writeline("@echo " + seperator);         while (!finished)         {             application.doevents();             system.threading.thread.sleep(100);         }     }      private void form1_formclosing(object sender, formclosingeventargs e)     {         finished = true;     } 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -