/// <summary> /// 将一个实现IThreadable的对象加入到主线程中,进行执行 /// </summary> /// <param name="_threadable"></param> public void GoThreading(IThreadable _threadable) { if (_threadable.AlwaysUpdate) { alwaysUpdateQueue.Add(_threadable); } else { var type = _threadable.Type; if (baseThreads.ContainsKey(type)) { if (!baseThreads[type].Contains(_threadable)) { baseThreads[type].Enqueue(_threadable); } else if (toCancel[type].Contains(_threadable)) { toCancel[type].Remove(_threadable); } else { Debug.LogWarningFormat("{0} {1},在正在执行的情况下进行了RequestTick操作,这可能存在潜在的逻辑错误", type.ToString(), _threadable.ToString()); } } else { //开辟新的线程 float maxTime = 0.001f; //1毫秒 var attrs = type.GetCustomAttributes(typeof(MainThreadingAttribute), true); if (attrs != null && attrs.Length > 0) { var attr = (MainThreadingAttribute)attrs[0]; maxTime = attr.MaxTime * 0.001f; if (!Application.isEditor) { maxTime *= (attr.ForFPS / 60); maxTime = Mathf.Min(attr.TopMostTime, maxTime); } } baseThreads.Add(type, new Queue <IThreadable>()); toCancel.Add(type, new HashSet <IThreadable>()); maxTimeConsumptions.Add(type, maxTime); //再执行一次,这样就直接开始了 GoThreading(_threadable); } } }
public void Add(string key, IThreadable thread) { if (_threadMap.TryRemove(key, out IThreadable beforeThread)) { beforeThread.Stop(); Console.WriteLine("Remove a thread."); LogManager.GetCurrentClassLogger().Info("Remove a thread."); } thread.Start(); if (_threadMap.TryAdd(key, thread)) { LogManager.GetCurrentClassLogger().Info("Add a thread."); } }
private int MAX_WAIT_TIME = 5 * 1000; // 5 seconds #endregion #region Constructor(s) public static ThreadHandler Instance(IThreadable threadableObject) { lock (_handlerListLock) { // It's better not to return null if shutdown already initiated // because most callers may not handle null, and that can result in an Exception. // The strategy is to return a new handler to the caller, but not let them start // a new thread string componentName = threadableObject.Name; if (!_threads.ContainsKey(componentName)) { var handler = new ThreadHandler(); handler._task = threadableObject.Operation; handler._waitHandle = threadableObject.WaitHandle; handler._abortHandle = threadableObject.AbortHandle; handler._cancelTaskToken = threadableObject.CancelTaskToken; _threads.Add(componentName, handler); } return(_threads[componentName]); } }
/// <summary> /// 取消一个对象的执行 /// 注意,如果这个对象是UnityObject,那么不要在OnDestroy中调用 /// 因为Threading检测到为空后,会自己删除掉 /// 如果是其他类型,那么必须调用,否则Threading会保留一份引用,导致内存泄漏 /// </summary> /// <param name="_threadable"></param> public void CancelThreading(IThreadable _threadable) { if (_threadable.AlwaysUpdate) { if (alwaysUpdateQueue.Count == 0) { return; } var index = alwaysUpdateQueue.IndexOf(_threadable); if (index != -1) { FastRemoveFromList(alwaysUpdateQueue, index); } } else { if (baseThreads.Count == 0) { return; } var type = _threadable.Type; if (toCancel.ContainsKey(type)) { if (!baseThreads[type].Contains(_threadable)) { Debug.LogWarningFormat("{0} {1},在没有执行的情况下进行了Quit操作,这没什么问题,但是尽量从逻辑上避免这种性能损耗。", type.ToString(), _threadable.ToString()); } else { toCancel[type].Add(_threadable); } } } }