| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- using System;
- using System.IO;
- using System.Threading;
- using System.Collections.Generic;
- public class StreamWriterProxy :
- IDisposable
- {
- StreamWriter Writer = null;
- bool bValid = false;
- public StreamWriterProxy(string InFilePath, bool bInCreateNew)
- {
- BackgroundWorker.Instance.AddBackgroudOperation(() => this.Reset_MT(InFilePath, bInCreateNew));
- bValid = true;
- }
- public void Flush()
- {
- BackgroundWorker.Instance.AddBackgroudOperation(() => this.Flush_MT());
- }
- public void Dispose()
- {
- BackgroundWorker.Instance.AddBackgroudOperation(() => this.Dispose_MT());
- bValid = false;
- }
- public void Close()
- {
- BackgroundWorker.Instance.AddBackgroudOperation(() => this.Close_MT());
- }
- public bool isValid
- {
- get
- {
- return bValid;
- }
- }
- public void WriteLine(string InText)
- {
- DebugHelper.Assert(bValid, "Should not WriteLine When the Proxy is not valid!");
- BackgroundWorker.Instance.AddBackgroudOperation(() => this.WriteLine_MT(InText));
- }
- protected void Flush_MT()
- {
- if (Writer != null)
- {
- Writer.Flush();
- }
- }
- protected void Reset_MT(string InFilePath, bool bInCreateNew)
- {
- try
- {
- FileStream fs = null;
- if (bInCreateNew)
- {
- fs = new FileStream(InFilePath, FileMode.Create, FileAccess.Write, FileShare.Read);
- }
- else
- {
- fs = new FileStream(InFilePath, FileMode.Append, FileAccess.Write, FileShare.Read);
- }
- Writer = new StreamWriter(fs);
- bValid = true;
- }
- catch (Exception)
- {
- // lost logs.
- #if DEBUG
- //UnityEngine.Debug.Break();
- #endif
- }
- }
- protected void Dispose_MT()
- {
- if (Writer != null)
- {
- Writer.Dispose();
- }
- }
- protected void Close_MT()
- {
- if (Writer != null)
- {
- Writer.Close();
- }
- }
- protected void WriteLine_MT(string InText)
- {
- if (Writer != null)
- {
- Writer.WriteLine(InText);
- }
- }
- }
- public class BackgroundWorker :
- Singleton<BackgroundWorker>
- {
- public delegate void BackgroudDelegate();
- Thread WorkingThread = null;
- bool bRequestExit = false;
- List<BackgroudDelegate> PendingWork = new List<BackgroudDelegate>();
- List<BackgroudDelegate> WorkingList = new List<BackgroudDelegate>();
- public int ThreadID = 0;
- public override void Init()
- {
- WorkingThread = new Thread(new ThreadStart(StaticEntry));
- ThreadID = WorkingThread.ManagedThreadId;
- WorkingThread.Start();
- }
- public override void UnInit()
- {
- bRequestExit = true;
- WorkingThread.Join();
- WorkingThread = null;
- }
- protected static void StaticEntry()
- {
- BackgroundWorker.Instance.Entry();
- }
- static void Swap<T>(ref T a, ref T b)
- {
- T t = a;
- a = b;
- b = t;
- }
- protected void Entry()
- {
- while (!bRequestExit)
- {
- {
- lock (PendingWork)
- {
- Swap(ref PendingWork, ref WorkingList);
- }
- }
- int Count = WorkingList.Count;
- for (int i = 0; i < Count; ++i)
- {
- try
- {
- WorkingList[i]();
- }
- catch (Exception e)
- {
- #if UNITY_EDITOR || UNITY_STANDALONE
- DebugHelper.Assert(false, "Exception in Background Working Thread, Message:{0}, Callstack:\n{1}",
- e.Message,
- e.StackTrace
- );
- #endif
- }
- }
- WorkingList.Clear();
- Thread.Sleep(60);
- }
- }
- public void AddBackgroudOperation(BackgroudDelegate InDelegate)
- {
- lock (PendingWork)
- {
- PendingWork.Add(InDelegate);
- }
- }
- }
|