ProcessManager.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS AS IS AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. */
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Text;
  31. using System.Diagnostics;
  32. namespace OpenSim.GUI
  33. {
  34. public class ProcessManager : Process
  35. {
  36. private string m_FileName;
  37. private string m_Arguments;
  38. public ProcessManager(string FileName,string Arguments)
  39. {
  40. m_FileName = FileName;
  41. m_Arguments = Arguments;
  42. // MyProc = new Process();
  43. StartInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
  44. Console.WriteLine("WorkingDirectory: " + StartInfo.WorkingDirectory);
  45. StartInfo.FileName = m_FileName;
  46. //p.StartInfo.Arguments = "";
  47. StartInfo.UseShellExecute = false;
  48. StartInfo.RedirectStandardError = true;
  49. StartInfo.RedirectStandardInput = true;
  50. StartInfo.RedirectStandardOutput = true;
  51. StartInfo.CreateNoWindow = true;
  52. }
  53. public void StartProcess()
  54. {
  55. try
  56. {
  57. Start();
  58. BeginOutputReadLine();
  59. BeginErrorReadLine();
  60. }
  61. catch (Exception ex)
  62. {
  63. Console.WriteLine("Exception Occurred :{0},{1}",
  64. ex.Message, ex.StackTrace.ToString());
  65. }
  66. }
  67. public void StopProcess()
  68. {
  69. try
  70. {
  71. CancelErrorRead();
  72. CancelErrorRead();
  73. if (!HasExited)
  74. {
  75. StandardInput.WriteLine("quit");
  76. StandardInput.WriteLine("shutdown");
  77. System.Threading.Thread.Sleep(500);
  78. if (!HasExited)
  79. {
  80. Kill();
  81. }
  82. }
  83. }
  84. catch (Exception ex)
  85. {
  86. Console.WriteLine("Exception Occurred :{0},{1}",
  87. ex.Message, ex.StackTrace.ToString());
  88. }
  89. }
  90. }
  91. }