AppExecutor_Thread.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 OpenSimulator 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. //using System;
  28. //using System.Collections;
  29. //using System.Collections.Generic;
  30. //using System.Reflection;
  31. //using System.Text;
  32. //using System.Threading;
  33. //using log4net;
  34. //namespace OpenSim.GridLaunch
  35. //{
  36. // internal class AppExecutor2
  37. // {
  38. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  39. // private static readonly int consoleReadIntervalMilliseconds = 50;
  40. // //private static readonly Timer readTimer = new Timer(readConsole, null, Timeout.Infinite, Timeout.Infinite);
  41. // private static Thread timerThread;
  42. // private static object timerThreadLock = new object();
  43. // #region Start / Stop timer thread
  44. // private static void timer_Start()
  45. // {
  46. // //readTimer.Change(0, consoleReadIntervalMilliseconds);
  47. // lock (timerThreadLock)
  48. // {
  49. // if (timerThread == null)
  50. // {
  51. // m_log.Debug("Starting timer thread.");
  52. // timerThread = new Thread(timerThreadLoop);
  53. // timerThread.Name = "StdOutputStdErrorReadThread";
  54. // timerThread.IsBackground = true;
  55. // timerThread.Start();
  56. // }
  57. // }
  58. // }
  59. // private static void timer_Stop()
  60. // {
  61. // //readTimer.Change(Timeout.Infinite, Timeout.Infinite);
  62. // lock (timerThreadLock)
  63. // {
  64. // if (timerThread != null)
  65. // {
  66. // m_log.Debug("Stopping timer thread.");
  67. // try
  68. // {
  69. // if (timerThread.IsAlive)
  70. // timerThread.Abort();
  71. // timerThread.Join(2000);
  72. // timerThread = null;
  73. // }
  74. // catch (Exception ex)
  75. // {
  76. // m_log.Error("Exception stopping timer thread: " + ex.ToString());
  77. // }
  78. // }
  79. // }
  80. // }
  81. // #endregion
  82. // #region Timer read from consoles and fire event
  83. // private static void timerThreadLoop()
  84. // {
  85. // try
  86. // {
  87. // while (true)
  88. // {
  89. // readConsole();
  90. // Thread.Sleep(consoleReadIntervalMilliseconds);
  91. // }
  92. // }
  93. // catch (ThreadAbortException) { } // Expected on thread shutdown
  94. // }
  95. // private static void readConsole()
  96. // {
  97. // try
  98. // {
  99. // // Lock so we don't collide with any startup or shutdown
  100. // lock (Program.AppList)
  101. // {
  102. // foreach (AppExecutor app in new ArrayList(Program.AppList.Values))
  103. // {
  104. // try
  105. // {
  106. // string txt = app.GetStdOutput();
  107. // // Fire event with received text
  108. // if (!string.IsNullOrEmpty(txt))
  109. // Program.FireAppConsoleOutput(app.File, txt);
  110. // }
  111. // catch (Exception ex)
  112. // {
  113. // m_log.ErrorFormat("Exception reading standard output from \"{0}\": {1}", app.File, ex.ToString());
  114. // }
  115. // try
  116. // {
  117. // string txt = app.GetStdError();
  118. // // Fire event with received text
  119. // if (!string.IsNullOrEmpty(txt))
  120. // Program.FireAppConsoleOutput(app.File, txt);
  121. // }
  122. // catch (Exception ex)
  123. // {
  124. // m_log.ErrorFormat("Exception reading standard error from \"{0}\": {1}", app.File, ex.ToString());
  125. // }
  126. // }
  127. // }
  128. // }
  129. // finally
  130. // {
  131. // }
  132. // }
  133. // #endregion
  134. // #region Read stdOutput and stdError
  135. // public string GetStdOutput()
  136. // {
  137. // return GetStreamData(Output);
  138. // }
  139. // public string GetStdError()
  140. // {
  141. // return GetStreamData(Error);
  142. // }
  143. // private static int num = 0;
  144. // // Gets any data from StreamReader object, non-blocking
  145. // private static string GetStreamData(StreamReader sr)
  146. // {
  147. // // Can't read?
  148. // if (!sr.BaseStream.CanRead)
  149. // return "";
  150. // // Read a chunk
  151. // //sr.BaseStream.ReadTimeout = 100;
  152. // byte[] buffer = new byte[4096];
  153. // num++;
  154. // Trace.WriteLine("Start read " + num);
  155. // int len = sr.BaseStream.Read(buffer, 0, buffer.Length);
  156. // Trace.WriteLine("End read " + num + ": " + len);
  157. // // Nothing?
  158. // if (len <= 0)
  159. // return "";
  160. // // Return data
  161. // StringBuilder sb = new StringBuilder();
  162. // sb.Append(System.Text.Encoding.ASCII.GetString(buffer, 0, len));
  163. // //while (sr.Peek() >= 0)
  164. // //{
  165. // // sb.Append(Convert.ToChar(sr.Read()));
  166. // //}
  167. // return sb.ToString();
  168. // }
  169. // #endregion
  170. // }
  171. //}