code_junkie

Hanging process when run with .NET Process.Start — what's wrong?

Nov 13th, 2011
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. svn list "http://myserver:84/svn/Documents/Instruments/" --xml --no-auth-cache --username myuser --password mypassword
  2.  
  3. string cmd = "svn.exe";
  4. string arguments = "list "http://myserver:84/svn/Documents/Instruments/" --xml --no-auth-cache --username myuser --password mypassword";
  5. int ms = 5000;
  6. ProcessStartInfo psi = new ProcessStartInfo(cmd);
  7. psi.Arguments = arguments;
  8. psi.RedirectStandardOutput = true;
  9. psi.WindowStyle = ProcessWindowStyle.Normal;
  10. psi.UseShellExecute = false;
  11. Process proc = Process.Start(psi);
  12. StreamReader output = new StreamReader(proc.StandardOutput.BaseStream, Encoding.UTF8);
  13.  
  14. proc.WaitForExit(ms);
  15. if (proc.HasExited)
  16. {
  17. return output.ReadToEnd();
  18. }
  19.  
  20. string cmd = "cmd";
  21. string arguments = "/S /C "" + exe + " " + args + """;
  22.  
  23. ProcessBufferHandler bufferHandler = new ProcessBufferHandler(proc.StandardOutput.BaseStream,
  24. Encoding.UTF8);
  25. ThreadPool.QueueUserWorkItem(ProcessStream, bufferHandler);
  26.  
  27. proc.WaitForExit(ms);
  28. if (proc.HasExited)
  29. {
  30. bufferHandler.Stop();
  31. return bufferHandler.ReadToEnd();
  32. }
  33.  
  34. private class ProcessBufferHandler
  35. {
  36. public Stream stream;
  37. public StringBuilder sb;
  38. public Encoding encoding;
  39. public State state;
  40.  
  41. public enum State
  42. {
  43. Running,
  44. Stopped
  45. }
  46.  
  47. public ProcessBufferHandler(Stream stream, Encoding encoding)
  48. {
  49. this.stream = stream;
  50. this.sb = new StringBuilder();
  51. this.encoding = encoding;
  52. state = State.Running;
  53. }
  54. public void ProcessBuffer()
  55. {
  56. sb.Append(new StreamReader(stream, encoding).ReadToEnd());
  57. }
  58.  
  59. public string ReadToEnd()
  60. {
  61. return sb.ToString();
  62. }
  63.  
  64. public void Stop()
  65. {
  66. state = State.Stopped;
  67. }
  68. }
  69.  
  70. Process command = new Process();
  71. command.EnableRaisingEvents = false;
  72. command.StartInfo.FileName = "svn.exe";
  73. command.StartInfo.Arguments = "your svn arguments here";
  74. command.StartInfo.UseShellExecute = false;
  75. command.StartInfo.RedirectStandardOutput = true;
  76. command.Start();
  77.  
  78. while (!command.StandardOutput.EndOfStream)
  79. {
  80. Console.WriteLine(command.StandardOutput.ReadLine());
  81. }
Add Comment
Please, Sign In to add comment