C# ProcessStartInfo always run duplicate process -


i have bit of code open cmd , run command output, it's running twice times, , output missing.

enter image description here

here code, re-check many times can't figure out cause.

using system; using system.diagnostics; using system.io; using system.security.cryptography; using system.security.permissions; using system.text; using system.threading;   namespace commandhandler {     class program     {         public static string change_file = appdomain.currentdomain.basedirectory + @"change\change.txt";         public static void main()         {             //console.setwindowsize(console.largestwindowwidth, console.largestwindowheight);             console.backgroundcolor = consolecolor.black;             console.foregroundcolor = consolecolor.green;              //processstartinfo startinfo = new processstartinfo("cmd", "/c php d:/test.php")             processstartinfo startinfo = new processstartinfo("cmd", "/c d:\\php64\\php d:\\xampp\\htdocs\\xxx\\bin\\listen.php")             {                 windowstyle = processwindowstyle.hidden,                 useshellexecute = false,                 redirectstandardoutput = true,                 createnowindow = true             };              process process = process.start(startinfo);             //process.outputdatareceived += new system.diagnostics.datareceivedeventhandler(proc_outputdatareceived);             process.outputdatareceived += new datareceivedeventhandler((sender, e) =>             {                 // prepend line numbers each line of output.                 if (!string.isnullorempty(e.data))                 {                     string value = e.data.tolower();                     console.writeline(e.data);                     if (value.contains("php fatal error:"))                     {                         string hash = md5(datetime.now.tostring());                         system.io.file.writealltext(change_file, hash);                     }                 }             });             process.beginoutputreadline();             process.start();             process.waitforexit();             console.readkey();         }          public static byte[] encryptdata(string data)         {             system.security.cryptography.md5cryptoserviceprovider md5hasher = new system.security.cryptography.md5cryptoserviceprovider();             byte[] hashedbytes;             system.text.utf8encoding encoder = new system.text.utf8encoding();             hashedbytes = md5hasher.computehash(encoder.getbytes(data));             return hashedbytes;         }          public static string md5(string data)         {             return bitconverter.tostring(encryptdata(data)).replace("-", "").tolower();         }     } } 

any idea?

because you're calling process.start() twice. here when create instance process:

process process = process.start(startinfo); 

and again here, near bottom of constructor:

process.start();  

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -