c# - Determining end of console output -


i receive console output in application. use code here (the accepted solution). never null in outputdatareceived. instead, have string.empty @ end of output. correct use string.nullorempty instead of comparing null?

static void main(string[] args)         {             var command = @"wmic cpu loadpercentage";             using (process process = new process())             {                 process.startinfo.filename = "cmd.exe";                 process.startinfo.useshellexecute = false;                 process.startinfo.createnowindow = true;                 process.startinfo.redirectstandardoutput = true;                 process.startinfo.redirectstandardinput = true;                 process.startinfo.redirectstandarderror = true;                  stringbuilder output = new stringbuilder();                 stringbuilder error = new stringbuilder();                  using (autoresetevent outputwaithandle = new autoresetevent(false))                 using (autoresetevent errorwaithandle = new autoresetevent(false))                 {                     process.outputdatareceived += (sndr, a) =>                     {                         if (a.data == null)                         {                             outputwaithandle.set();                         }                         else                         {                             output.appendline(a.data);                         }                     };                     process.errordatareceived += (sndr, a) =>                     {                         if (a.data == null)                         {                             errorwaithandle.set();                         }                         else                         {                             error.appendline(a.data);                         }                     };                      process.start();                     process.beginoutputreadline();                         outputwaithandle.waitone();                      string path = "test.txt";                     using (streamwriter sw = file.exists(path) ? file.appendtext(path) : file.createtext(path))                     {                         sw.writeline(string.format("{0}, {1}", datetime.now, output));                      }                  }             }         } 

update: seems won't work multiline output. question why there's no null in a.data

the standard output stream terminated process terminated. note here, "process" means process running cmd.exe. process can start other processes, have own standard output stream, , while process's stdout terminated when terminate, none of affects parent cmd.exe process's stdout.

if want run process runs various other processes emulating user input cmd.exe, have emulate user interaction recognize , respond final command being executed.

imagine in real user's place , think how deal situation. when execute command in command-prompt window, window close when command has completed? no. receive sort of indication command has completed? sort of. first, presumably expected output of command conform specific format, include indicator command has completed. second, command-prompt window display new prompt (i.e. "prompt" in phrase "command-prompt").

note malicious command figure out current prompt looks , fake it. that's unusual , presumably have enough control on commands you're issuing avoid that. 1 approach process output occurs , detect new prompt appears when command has completed.

if doesn't seem reliable enough, have handle each command individually, interpreting output command , recognizing when command has reached end of output solely content in output.

finally note can in fact use process class execute commands themselves. don't need cmd.exe that. , if execute each command separate process yourself, do wind getting notification of termination of process via end-of-stream of stdout each process.


Comments

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -