C#: Calling a php script and beeing able to stop it -


i working on c# program needs call local php script , write output file. problem is, need able stop execution of script.

first, tried call cmd.exe , let cmd write output file worked fine. found out, killing cmd process not stop php cli.

so tried call php directly, redirect output , write c# code file. here problem seems be, the php cli not terminate when script done. process.waitforexit() not return, when sure script has been executed.

i cannot set timeout waitforexit(), because depending on arguments, script may take 3 minutes or eg. 10 hours.

i not want kill random php cli, there may others running.

what best way call local php script c#, writing output file , beeing able stop execution?

here current code:

// create process var process = new system.diagnostics.process(); process.enableraisingevents = true; process.startinfo.useshellexecute = false; process.startinfo.filename = "php.exe"; // createexportscriptargument returns "file.php arg1 arg2 ..." process.startinfo.arguments = createexportscriptargument(code, this.content, this.options); process.startinfo.windowstyle = system.diagnostics.processwindowstyle.hidden; process.startinfo.redirectstandardoutput = true;  // start process or cancel, if process should not run if (!this.isrunning) { return; } this.currentprocess = process; process.start();  // output var output = process.standardoutput;  // wait process finish process.waitforexit(); this.currentprocess = null; 

to kill process using:

// mark not running prevent starting new this.isrunning = false;  // kill process if (this.currentprocess != null) {   this.currentprocess.kill(); } 

thanks reading!

edit

that cli not return seems not reproducible. when test different script (without arguments) works, script or passing of arguments.

running script cmd works fine, script should not problem

edit 2

when disabling redirectstandardoutput, cli quits. be, need read output, before process finishes? or process wait, when kind of buffer full?

edit 3: problem solved

thanks volkerk, / found solution. problem was, waitforexit() did not called, when output not read (probably due full buffer in standard output). script wrote output.

what works me:

process.start();  // output var output = process.standardoutput;  // read input , write file, live avoid reading / writing @ once using (var file = new streamwriter("path\\file", false, new utf8encoding())) {     // read each line     while (!process.hasexited)     {         file.writeline(output.readline());     }      // read rest     file.write(output.readtoend());      // flush file     file.flush(); } 

since problem output buffer full , therefore php process stalled while waiting send output, asynchronously reading output in c# program solution.

class program {     protected static /* yeah, yeah, it's example */ stringbuilder output;     static void main(string[] args)     {         // create process         var process = new system.diagnostics.process();         process.enableraisingevents = true;         process.startinfo.useshellexecute = false;         process.startinfo.filename = "php.exe";         process.startinfo.arguments = "-f path\\test.php mu b 0 0 pgsql://user:pass@x.x.x.x:5432/nominatim";         process.startinfo.windowstyle = system.diagnostics.processwindowstyle.hidden;         process.startinfo.redirectstandardoutput = true;          output = new stringbuilder();         process.outputdatareceived += process_outputdatareceived;          // start process         process.start();         process.beginoutputreadline();          // wait process finish         process.waitforexit();          console.writeline("test");         // <-- program.output here -->          console.readkey();     }      static void process_outputdatareceived(object sender, system.diagnostics.datareceivedeventargs e)     {         if (!string.isnullorempty(e.data)) {             // edit: oops new-line/carriage-return characters not "in" e.data.....             // _might_ problem depending on actual output.             output.append(e.data);             output.append(environment.newline);         }     } } 

see also: https://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline%28v=vs.110%29.aspx


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -