.net - System.Diagnostics.Process.Kill doesn't work properly. C# -


i have winform (.net 4.5 c#) application have backgroundworker start new process using code below.

    void bw_dowork(object sender, doworkeventargs e)     {         backgroundworker bw = sender backgroundworker;          string filename_ = e.argument.tostring();          process myprocess = new process();         int exitcode = 0;          try         {             if (!file.exists(filename_))             {                 throw new filenotfoundexception("failed locate " + filename_);              }               #region start info:             processstartinfo startinfo = new processstartinfo(filename_);             startinfo.useshellexecute = false;             startinfo.createnowindow = true;             startinfo.redirectstandardoutput = true;             startinfo.redirectstandarderror = true;              myprocess.startinfo = startinfo;              #endregion              myprocess.start();             streamreader mystreamreader = myprocess.standardoutput;              while (!myprocess.hasexited)             {                 myprocess.refresh();                 string output = mystreamreader.readline();                 bw.reportprogress(0, output);                  if (bw.cancellationpending)                 {                     mystreamreader.readtoend();                     mystreamreader.close();                      myprocess.standarderror.readtoend();                     myprocess.standarderror.close();                     myprocess.kill();                     break;                  }              }               //myprocess.waitforexit();               bw.reportprogress(0, string.format("process {0}  exit code: {1}", filename_, myprocess.exitcode));              exitcode = myprocess.exitcode;             if (exitcode != 0 && !bw.cancellationpending)             {                 string error = myprocess.standarderror.readtoend();                   myprocess.close();                 myprocess = null;                  bw.reportprogress(0, "process failed message:" + environment.newline + error);             }              myprocess.close();          }         catch (exception ex)         {                              console.writeline(ex);          }                 {             myprocess.dispose();             myprocess = null;          }          e.result = exitcode;     } 

this part works fine, once try use myprocess.kill(); background worker stops , ok, can see process still running task manager, if close application can still see in task manager process running.

my question is, how kill process?

i hope described problem clearly, if not feel free let me know if need more information.

any appreciated.

//--------------update dec. 8th 2014 ---------------------//

@rogern removed process.waitforexit() , added mystreamreader.readtoend() same issue process still alive after application closed.

@phillip console application (stand alone *.exe) call winform.

@peter duniho yes sure it's same process in task manager. removing waitforexit() didn't solve anything, bw in fact completed fine without issue.

@l.b alternative suggest?

deadlock can occur because calling process.waitforexit() before reading entire output stream. since you've redirected output stream must read data associated stream before process can exit normally.


Comments