StreamWriterProxy.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using System.Collections.Generic;
  5. public class StreamWriterProxy :
  6. IDisposable
  7. {
  8. StreamWriter Writer = null;
  9. bool bValid = false;
  10. public StreamWriterProxy(string InFilePath, bool bInCreateNew)
  11. {
  12. BackgroundWorker.Instance.AddBackgroudOperation(() => this.Reset_MT(InFilePath, bInCreateNew));
  13. bValid = true;
  14. }
  15. public void Flush()
  16. {
  17. BackgroundWorker.Instance.AddBackgroudOperation(() => this.Flush_MT());
  18. }
  19. public void Dispose()
  20. {
  21. BackgroundWorker.Instance.AddBackgroudOperation(() => this.Dispose_MT());
  22. bValid = false;
  23. }
  24. public void Close()
  25. {
  26. BackgroundWorker.Instance.AddBackgroudOperation(() => this.Close_MT());
  27. }
  28. public bool isValid
  29. {
  30. get
  31. {
  32. return bValid;
  33. }
  34. }
  35. public void WriteLine(string InText)
  36. {
  37. DebugHelper.Assert(bValid, "Should not WriteLine When the Proxy is not valid!");
  38. BackgroundWorker.Instance.AddBackgroudOperation(() => this.WriteLine_MT(InText));
  39. }
  40. protected void Flush_MT()
  41. {
  42. if (Writer != null)
  43. {
  44. Writer.Flush();
  45. }
  46. }
  47. protected void Reset_MT(string InFilePath, bool bInCreateNew)
  48. {
  49. try
  50. {
  51. FileStream fs = null;
  52. if (bInCreateNew)
  53. {
  54. fs = new FileStream(InFilePath, FileMode.Create, FileAccess.Write, FileShare.Read);
  55. }
  56. else
  57. {
  58. fs = new FileStream(InFilePath, FileMode.Append, FileAccess.Write, FileShare.Read);
  59. }
  60. Writer = new StreamWriter(fs);
  61. bValid = true;
  62. }
  63. catch (Exception)
  64. {
  65. // lost logs.
  66. #if DEBUG
  67. //UnityEngine.Debug.Break();
  68. #endif
  69. }
  70. }
  71. protected void Dispose_MT()
  72. {
  73. if (Writer != null)
  74. {
  75. Writer.Dispose();
  76. }
  77. }
  78. protected void Close_MT()
  79. {
  80. if (Writer != null)
  81. {
  82. Writer.Close();
  83. }
  84. }
  85. protected void WriteLine_MT(string InText)
  86. {
  87. if (Writer != null)
  88. {
  89. Writer.WriteLine(InText);
  90. }
  91. }
  92. }
  93. public class BackgroundWorker :
  94. Singleton<BackgroundWorker>
  95. {
  96. public delegate void BackgroudDelegate();
  97. Thread WorkingThread = null;
  98. bool bRequestExit = false;
  99. List<BackgroudDelegate> PendingWork = new List<BackgroudDelegate>();
  100. List<BackgroudDelegate> WorkingList = new List<BackgroudDelegate>();
  101. public int ThreadID = 0;
  102. public override void Init()
  103. {
  104. WorkingThread = new Thread(new ThreadStart(StaticEntry));
  105. ThreadID = WorkingThread.ManagedThreadId;
  106. WorkingThread.Start();
  107. }
  108. public override void UnInit()
  109. {
  110. bRequestExit = true;
  111. WorkingThread.Join();
  112. WorkingThread = null;
  113. }
  114. protected static void StaticEntry()
  115. {
  116. BackgroundWorker.Instance.Entry();
  117. }
  118. static void Swap<T>(ref T a, ref T b)
  119. {
  120. T t = a;
  121. a = b;
  122. b = t;
  123. }
  124. protected void Entry()
  125. {
  126. while (!bRequestExit)
  127. {
  128. {
  129. lock (PendingWork)
  130. {
  131. Swap(ref PendingWork, ref WorkingList);
  132. }
  133. }
  134. int Count = WorkingList.Count;
  135. for (int i = 0; i < Count; ++i)
  136. {
  137. try
  138. {
  139. WorkingList[i]();
  140. }
  141. catch (Exception e)
  142. {
  143. #if UNITY_EDITOR || UNITY_STANDALONE
  144. DebugHelper.Assert(false, "Exception in Background Working Thread, Message:{0}, Callstack:\n{1}",
  145. e.Message,
  146. e.StackTrace
  147. );
  148. #endif
  149. }
  150. }
  151. WorkingList.Clear();
  152. Thread.Sleep(60);
  153. }
  154. }
  155. public void AddBackgroudOperation(BackgroudDelegate InDelegate)
  156. {
  157. lock (PendingWork)
  158. {
  159. PendingWork.Add(InDelegate);
  160. }
  161. }
  162. }