public void Init(ThreadDispatchDelegateArgReturn dispatchCall, object dispatchArgumentParameter, bool safeMode) { this.safeMode = safeMode; this.dispatchCallArgReturn = dispatchCall; this.dispatchArgParam = dispatchArgumentParameter; ValidateExecutionOnInit(true); //Forced to wait, the return-result should always be available after execution }
/// <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) { 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); }
/// <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) { return(MainThreadDispatcher.DispatchToMainThreadReturn(dispatchCall, dispatchArgument, safeMode)); }
/// <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) { 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; }