c# - Diagnostic.Process merge StandardOutput and StandardErrorOutput -


i'm running cmd.exe .net program using process class , passing in various commands execute programs i.e. ipconfig, netstat etc. issue these programs output error text, sent standarderror, there no way of knowing during standard output error printed. error message separately. possible combine standardoutput , standarderror 1 synchronized stream data in order? if not, how organize output 2 different streams it's in proper order?

well. guess can't expect stdout , stderr come in exact order have been output. because @ system level, stdout , stderr have own buffer respectively. can imagine such case -

(1) when 'command' writes stdout first, buffer might not flushed immediately.

(2) 'command' writes stderr, however, time buffer stderr might flushed.

in case, though (1) comes first, (2) comes first.

hope following snippet -

(improved code)

    class outputcontext     {         public const int buffersize = 1024;          public bool iseof { get; set; }         public bool iswaiting { get; set; }         public byte[] buffer { get; set; }         public streamreader reader { get; set; }         public object tag { get; set; }          public outputcontext(streamreader r, object tag)         {             iseof = false;             iswaiting = false;             buffer = new byte[buffersize];             reader = r;             tag = tag;         }     }      process proc;      void callback(iasyncresult ar)     {         lock (ar.asyncstate)         {             outputcontext ctx = ar.asyncstate outputcontext;             int c = ctx.reader.basestream.endread(ar);             ctx.iswaiting = false;              if (c == 0)             {                 ctx.iseof = true;                 return;             }              string content = encoding.utf8.getstring(ctx.buffer, 0, c);             console.write(content);             console.out.flush();          }     }      void redirectoutput(outputcontext ctx)     {         lock (ctx)         {             if (ctx.iseof)             {                 return;             }              if (ctx.iswaiting)             {                 return;             }              ctx.iswaiting = true;             iasyncresult ar = ctx.reader.basestream.beginread(ctx.buffer, 0,                  outputcontext.buffersize, callback, ctx);         }     }      void run(string yourprog, string yourargs)     {         // if gui app, shall not run on ui thread.         // spin new thread handle , wait thread complete.         // , can accept input long ui thread not         // blocked, , redirect input target proc's stdin asycly.          proc = new process();         proc.startinfo.createnowindow = true;         proc.startinfo.filename = yourprog;         proc.startinfo.arguments = yourargs;         proc.startinfo.useshellexecute = false;          proc.startinfo.redirectstandardoutput = true;         proc.startinfo.redirectstandarderror = true;         proc.startinfo.redirectstandardinput = true;          proc.start();          outputcontext stdoutctx = new outputcontext(proc.standardoutput, "stdout");         outputcontext stderrctx = new outputcontext(proc.standarderror, "stderr");          while (!stdoutctx.iseof && !stderrctx.iseof)         {             redirectoutput(stdoutctx);             redirectoutput(stderrctx);         }          proc.waitforexit();     } } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -