191 lines
4.3 KiB
C#
191 lines
4.3 KiB
C#
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);
|
|
}
|
|
}
|
|
} |