public static void Init() { SingleThreadStarter.Init(); MainThreadWatchdog.Init(); MainThreadDispatcher.Init(); UnityActivityWatchdog.Init(); }
//--------------------------------------- UNITY MONOBEHAVIOUR COMMANDS -------------------------------------- //--------------------------------------- UNITY MONOBEHAVIOUR COMMANDS -------------------------------------- //--------------------------------------- UNITY COROUTINE & PROVIDER-THREAD IMPLEMENTATION -------------------------------------- //--------------------------------------- UNITY COROUTINE & PROVIDER-THREAD IMPLEMENTATION -------------------------------------- #region UNITY COROUTINE & PROVIDER-THREAD IMPLEMENTATION /// <summary> /// Unlike "StartMultithreadedWorkloadExecution", you will have to build your own IThreadWorkerObject. /// Downside: It requires some extra work. Upside: you got more controll over what goes in and comes out /// Infact: You can create you own polymorphed IThreadWorkerObject-array, each ellement being a completely different type. For example: the statemachines of enemies are IThreadWorkerObject's and the array contains completely different classes with enemies/AI-behaviours. /// </summary> /// <param name="workerObjects">An array of IThreadWorkerObject objects to be handled by the threads. If you want multiple cores/threads to be active, make sure that the number of IThreadWorkerObject's proves matches/exeeds your preferred number maxWorkingThreads. </param> /// <param name="onComplete">Fired when all re-packaged workLoad-objects are finished computing</param> /// <param name="onPackageExecuted">Fires foreach finished re-packaged set of workLoad-object</param> /// <param name="maxThreads"> Lets you choose how many threads will be run simultaneously by the threadpool. Default: -1 == number of cores minus one, to make sure the MainThread has at least one core to run on. (quadcore == 1 core Mainthread, 3 cores used by the ThreadPoolScheduler)</param> /// <param name="scheduler">If Null, a new ThreadPoolScheduler will be instantiated.</param> /// <param name="safeMode">Executes all the computations within try-catch events, logging it the message + stacktrace</param> public void StartASyncThreads(IThreadWorkerObject[] workerObjects, ThreadPoolSchedulerEvent onCompleteCallBack, ThreadedWorkCompleteEvent onPackageExecuted = null, int maxThreads = -1, bool safeMode = true) { MainThreadWatchdog.Init(); MainThreadDispatcher.Init(); Debug.Log("ExecuteThreadedWork " + workerObjects.Length); if (_shedularBusy) { Debug.LogError("You are trying the start a new ASync threading-process, but is still Busy!"); return; } if (workerObjects == null || workerObjects.Length == 0) { Debug.LogError("Please provide an Array with atleast \"IThreadWorkerObject\"-object!"); return; } if (!ForceToMainThread) { _isAborted = false; this.onCompleteCallBack = onCompleteCallBack; this.onWorkerObjectDoneCallBack = onPackageExecuted; _shedularBusy = true; _providerThreadBusy = true; StartCoroutine("WaitForCompletion"); //--------------- Start Waiting for the Provider-thread to complete -------------------- workData = new ASyncThreadWorkData(workerObjects, safeMode, maxThreads); Thread providerThread = new Thread(new ThreadStart(InvokeASyncThreadPoolWork)); providerThread.Start(); //--------------- Start Waiting for the Provider-thread to complete -------------------- } else { //--------------- Execute all work in one bunch! -------------------- StartCoroutine(WaitAndExecuteWorkerObjects(workerObjects)); //--------------- Execute all work in one bunch! -------------------- } }
//--------------- Private Session Variables -------------------- //--------------------------------------- UNITY MONOBEHAVIOUR COMMANDS -------------------------------------- //--------------------------------------- UNITY MONOBEHAVIOUR COMMANDS -------------------------------------- #region UNITY MONOBEHAVIOUR COMMANDS protected virtual void Awake() { MainThreadWatchdog.Init(); MainThreadDispatcher.Init(); UnityActivityWatchdog.Init(); }
//--------------------------------------- CHECK UNITY ACTIVITY -------------------------------------- //--------------------------------------- CHECK UNITY ACTIVITY -------------------------------------- //--------------------------------------- MAIN THREAD WATCHDOG -------------------------------------- //--------------------------------------- MAIN THREAD WATCHDOG -------------------------------------- #region MAIN THREAD WATCHDOG /// <summary> /// If you need your current code to be running on the MainThread, you can always call this method to check if its the MainThread or not... /// </summary> /// <returns>Returns TRUE if its the MainThread</returns> public static bool CheckIfMainThread() { return(MainThreadWatchdog.CheckIfMainThread()); }