using UnityEngine; using System; using System.Collections.Generic; using Jobberwocky.GeometryAlgorithms.Source.Parameters; using Jobberwocky.GeometryAlgorithms.Source.Core; namespace Jobberwocky.GeometryAlgorithms.Source.API { public abstract class ThreadingAPI { protected static List ThreadingResultQueue = new List(); /// /// Activate the available callbacks /// public void ActivateCallbacks() { for (var i = 0; i < ThreadingResultQueue.Count; i++) { lock (ThreadingResultQueue) { var result = ThreadingResultQueue[i]; result.Callback(result.Output); ThreadingResultQueue.RemoveAt(i); } } } /// /// Starts the threading worker given the provided method /// /// /// /// /// protected static void StartWorker(Func, ThreadingResult> method, IParameters parameters, Action callback) { method.BeginInvoke(parameters, callback, WorkerCompleted, method); } /// /// Method that is called when the threading process is finished. This will call the provided callback function /// /// protected static void WorkerCompleted(IAsyncResult method) { var target = (Func, ThreadingResult>)method.AsyncState; var threadingResult = target.EndInvoke(method); ThreadingResultQueue.Add(threadingResult); } /// /// Object that is used to store the result when threading is used /// protected class ThreadingResult { public Action Callback; public Geometry Output; public ThreadingResult(Action callback, Geometry output) { Callback = callback; Output = output; } } } }