// Update is called once per frame void OnUpdate() { while (true) { lock (m_lockObj) { if (events.Count > 0) { ThreadEvent e = events.Dequeue(); try { switch (e.Key) { case NotiConst.UPDATE_EXTRACT: { //解压文件 OnExtractFile(e.evParams); } break; case NotiConst.UPDATE_DOWNLOAD: { //下载文件 OnDownloadFile(e.evParams); } break; } } catch (System.Exception ex) { UnityEngine.Debug.LogError(ex.Message); } } } Thread.Sleep(1); } }
/// <summary> /// 添加到事件队列 /// </summary> public void AddEvent(ThreadEvent ev, Action <NotiData> func) { lock (m_lockObj) { this.func = func; events.Enqueue(ev); } }
/// <summary> /// 添加到事件队列 /// </summary> public void AddEvent(ThreadEvent ev, Action <NotiData> func) { //lock (m_lockObject) //{ // this.func = func; // events.Enqueue(ev); //} }
public void AddEvent(ThreadEvent ev, Action <NotiData> func) { object lockObject = ThreadManager.m_lockObject; lock (lockObject) { this.func = func; ThreadManager.events.Enqueue(ev); } }
private void OnUpdate() { while (true) { object lockObject = ThreadManager.m_lockObject; lock (lockObject) { if (ThreadManager.events.Count > 0) { ThreadEvent threadEvent = ThreadManager.events.Dequeue(); try { string key = threadEvent.Key; if (key != null) { if (ThreadManager.< > f__switch$map7 == null) { ThreadManager.< > f__switch$map7 = new Dictionary <string, int>(2) { { "UpdateExtract", 0 }, { "UpdateDownload", 1 } }; } int num; if (ThreadManager.< > f__switch$map7.TryGetValue(key, out num)) { if (num != 0) { if (num == 1) { this.OnDownloadFile(threadEvent.evParams); } } else { this.OnExtractFile(threadEvent.evParams); } } } } catch (Exception ex) { Debug.LogError(ex.Message); } } } Thread.Sleep(1); } }
/// <summary> /// 线程下载 /// </summary> void BeginDownload(string url, string file) //线程下载 { object[] param = new object[2] { url, file }; ThreadEvent ev = new ThreadEvent(); ev.Key = NotiConst.UPDATE_DOWNLOAD; ev.evParams.AddRange(param); ThreadManager.AddEvent(ev, OnThreadCompleted); //线程下载 }
void BeginDownloadExtract(string dataPath, string dataName, byte[] buffer) { totalExtractCount += 1; if (!extractFiles.Contains(dataName)) { extractFiles.Add(dataName); } object[] param = new object[3] { dataPath, dataName, buffer }; ThreadEvent ev = new ThreadEvent(); ev.Key = NotiConst.DOWNLOAD_EXTRACT; ev.evParams.AddRange(param); ev.func = OnThreadCompleted; ThreadManager.AddEvent(ev); //线程解压 }
public void AddEvent(ThreadEvent ev) { lock (m_lockObject) { switch (ev.Key) { case NotiConst.DOWNLOAD_EXTRACT: { SmartThreadPool.Instance.QueueWorkItem(() => { //下载文件 OnExtractDownloadFile(ev.evParams, ev.func); }); //ThreadPool.QueueUserWorkItem((i) => //{ // //下载文件 // OnExtractDownloadFile(ev.evParams, ev.func); //}); } break; } } }