/// <summary> /// Fire and forget: The MainThread will execute this method witout any arguments to pass, nothing will be returned. /// </summary> /// <param name="dispatchCall">Example: "() => Debug.Log("This will be fired from the MainThread: " + System.Threading.Thread.CurrentThread.Name)" </param> /// <param name="waitForExecution">Freezes the thread, waiting for the MainThread to execute & finish the "dispatchCall".</param> /// <param name="safeMode">Executes all the computations within try-catch events, logging it the message + stacktrace</param> public static void DispatchToMainThread(ThreadDispatchDelegate dispatchCall, bool waitForExecution = false, bool safeMode = true) { ThreadDispatchAction action = new ThreadDispatchAction(); lock (dispatchActions) { dispatchActions.Add(action); } action.Init(dispatchCall, waitForExecution, safeMode); }
/// <summary> /// Fire and forget: The MainThread will execute this method witout any arguments to pass, nothing will be returned. /// </summary> /// <param name="dispatchCall">Example: "() => Debug.Log("This will be fired from the MainThread: " + System.Threading.Thread.CurrentThread.Name)" </param> /// <param name="waitForExecution">Freezes the thread, waiting for the MainThread to execute & finish the "dispatchCall".</param> /// <param name="safeMode">Executes all the computations within try-catch events, logging it the message + stacktrace</param> public static void DispatchToMainThread(ThreadDispatchDelegate dispatchCall, bool waitForExecution = false, bool safeMode = true) { if (MainThreadWatchdog.CheckIfMainThread()) { if (dispatchCall != null) dispatchCall(); } else { ThreadDispatchAction action = new ThreadDispatchAction(); lock (dispatchActions) { dispatchActions.Add(action); } action.Init(dispatchCall, waitForExecution, safeMode); } }
/// <summary> /// Allows you to dispatch an delegate returning an object (for example: a newly instantiated gameobject) that is directly available in your ThreadPool-Thread. /// Because the thread you are dispatching from is not the MainThread, your ThreadPool-thread needs to wait till the MainThread executed the method, and the returned value can be used directly /// </summary> /// <param name="dispatchCall">Example: "(object obj) => Debug.Log("This will be fired from the MainThread: " + System.Threading.Thread.CurrentThread.Name + "\nObject: " + obj.toString())"</param> /// <param name="safeMode">Executes all the computations within try-catch events, logging it the message + stacktrace</param> /// <returns>After the MainThread has executed the "dispatchCall" (and the worker-thread has been waiting), it will return whatever the dispatchCall returns to the worker-thread</returns> public static object DispatchToMainThreadReturn(ThreadDispatchDelegateReturn dispatchCall, bool safeMode = true) { ThreadDispatchAction action = new ThreadDispatchAction(); lock (dispatchActions) { dispatchActions.Add(action); } action.Init(dispatchCall, safeMode); //Puts the Thread to sleep while waiting for the action to be invoked. return action.dispatchExecutionResult; }
/// <summary> /// When executed by the MainThread, this argument will be passed to the "dispatchCall"; /// Allows you to dispatch an delegate returning an object (for example: a newly instantiated gameobject) that is directly available in your ThreadPool-Thread. /// Because the thread you are dispatching from is not the MainThread, your ThreadPool-thread needs to wait till the MainThread executed the method, and the returned value can be used directly /// </summary> /// <param name="dispatchCall">Example: "(object obj) => Debug.Log("This will be fired from the MainThread: " + System.Threading.Thread.CurrentThread.Name + "\nObject: " + obj.toString())"</param> /// <param name="dispatchArgument">Once the MainThread executes this action, the argument will be passed to the delegate</param> /// <param name="safeMode">Executes all the computations within try-catch events, logging it the message + stacktrace</param> /// <returns>After the MainThread has executed the "dispatchCall" (and the worker-thread has been waiting), it will return whatever the dispatchCall returns to the worker-thread</returns> public static object DispatchToMainThreadReturn(ThreadDispatchDelegateArgReturn dispatchCall, object dispatchArgument, bool safeMode = true) { if (MainThreadWatchdog.CheckIfMainThread()) { if (dispatchCall != null) return dispatchCall(dispatchArgument); } else { ThreadDispatchAction action = new ThreadDispatchAction(); lock (dispatchActions) { dispatchActions.Add(action); } action.Init(dispatchCall, dispatchArgument, safeMode); //Puts the Thread to sleep while waiting for the action to be invoked. return action.dispatchExecutionResult; } return null; }