Featured post
c# - strange behaviour on another process via Process.Start(startInfo) -- continue -
the original location (strange behaviour on process via process.start(startinfo)) doesn't allow me post test code properly. have start new question here.
our c#(v3.5) needs call c++ executable process raw data file , generate result files us.
it worked before following code (without readtoend() or readline() call):
startinfo.useshellexecute = false; startinfo.redirectstandarderror = true; startinfo.redirectstandardoutput = true;
now input raw data file changed (bigger before). our call executable hang forever. based on advice, added readtoend() or readline() hang on calls. have either use startinfo.useshellexecute = true;
or set
startinfo.useshellexecute = false; // , startinfo.redirectstandarderror = false; startinfo.redirectstandardoutput = false;
don't know why?
following cases tested:
working case:
processstartinfo startinfo = new processstartinfo(); startinfo.createnowindow = true; startinfo.useshellexecute = false; startinfo.redirectstandarderror = false; startinfo.redirectstandardoutput = false; startinfo.windowstyle = processwindowstyle.hidden; startinfo.filename = path; startinfo.arguments = rawdatafilename; //startinfo.workingdirectory = util.getparentdirectory(path, 1); try { process correctionprocess = process.start(startinfo); correctionprocess.waitforexit(); }
working case:
processstartinfo startinfo = new processstartinfo(); startinfo.createnowindow = true; startinfo.useshellexecute = true; startinfo.redirectstandarderror = false;//can commented out startinfo.redirectstandardoutput = false;//can commented out startinfo.windowstyle = processwindowstyle.hidden; startinfo.filename = path; startinfo.arguments = rawdatafilename; startinfo.workingdirectory = util.getparentdirectory(path, 1); try { process correctionprocess = process.start(startinfo); correctionprocess.waitforexit(); }
not working case (using
readline()
):processstartinfo startinfo = new processstartinfo(); startinfo.createnowindow = true; startinfo.useshellexecute = false; startinfo.redirectstandarderror = true; startinfo.redirectstandardoutput = true; startinfo.windowstyle = processwindowstyle.hidden; startinfo.filename = path; startinfo.arguments = rawdatafilename; //startinfo.workingdirectory = util.getparentdirectory(path, 1); try { process correctionprocess = process.start(startinfo); while (!correctionprocess.hasexited) { log.logitem(logtype.performance, "read errormsg @@@@", "dptm::correctdata()", ""); string errormsg = correctionprocess.standarderror.readline(); // <-- hangs here log.logitem(logtype.performance, "read errormsg", "dptm::correctdata()", "errormsg=" + errormsg); string outputmsg = correctionprocess.standardoutput.readline(); log.logitem(logtype.performance, "read outputmsg", "dptm::correctdata()", "outputmsg=" + outputmsg); thread.sleep(100); } }
not working case (using
readtoend()
):processstartinfo startinfo = new processstartinfo(); startinfo.createnowindow = true; startinfo.useshellexecute = false; startinfo.redirectstandarderror = true; startinfo.redirectstandardoutput = true; startinfo.windowstyle = processwindowstyle.hidden; startinfo.filename = path; startinfo.arguments = rawdatafilename; //startinfo.workingdirectory = util.getparentdirectory(path, 1); try { process correctionprocess = process.start(startinfo); while (!correctionprocess.hasexited) { log.logitem(logtype.performance, "read errormsg @@@@", "dptm::correctdata()", ""); string errormsg = correctionprocess.standarderror.readtoend(); // <-- hangs here! log.logitem(logtype.performance, "read errormsg", "dptm::correctdata()", "errormsg=" + errormsg); string outputmsg = correctionprocess.standardoutput.readtoend(); log.logitem(logtype.performance, "read outputmsg", "dptm::correctdata()", "outputmsg=" + outputmsg); thread.sleep(100); } }
not working case:
processstartinfo startinfo = new processstartinfo(); startinfo.createnowindow = true; startinfo.useshellexecute = false; startinfo.redirectstandarderror = true; startinfo.redirectstandardoutput = true; startinfo.windowstyle = processwindowstyle.hidden; startinfo.filename = path; startinfo.arguments = rawdatafilename; //startinfo.workingdirectory = util.getparentdirectory(path, 1); try { process correctionprocess = process.start(startinfo); log.logitem(logtype.performance, "read errormsg @@@@", "dptm::correctdata()", ""); string errormsg = correctionprocess.standarderror.readtoend(); // <-- hangs here! log.logitem(logtype.performance, "read errormsg", "dptm::correctdata()", "errormsg=" + errormsg); string outputmsg = correctionprocess.standardoutput.readtoend(); log.logitem(logtype.performance, "read outputmsg", "dptm::correctdata()", "outputmsg=" + outputmsg); correctionprocess.waitforexit(); }
you deadlocked on error/output buffer. second process waiting buffer free while waiting process finish. need asynchronously read output/error streams prevent that.
looking @ msdn sample: wow sample confusing... would've been easier follow if limited std out or std error.
you need first set data handler
correctionprocess.outputdatareceived += new datareceivedeventhandler(outputdatahandler);
and call
p.beginoutputreadline();
just comment [confusingly] advises no try read output there. use handler instead.
private void outputdatahandler(object sendingprocess, datareceivedeventargs data) { if (data.data != null) { // append data.data internal buffer (stringbuilder?) } }
- Get link
- X
- Other Apps
Comments
Post a Comment