예제 #1
0
        /// <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);
        }
예제 #2
0
 /// <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)
 {
     if (MainThreadWatchdog.CheckIfMainThread())
     {
         if (dispatchCall != null)
         {
             return(dispatchCall());
         }
     }
     else
     {
         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);
     }
     return(null);
 }
 public void Init(ThreadDispatchDelegateReturn dispatchCall, bool safeMode)
 {
     this.safeMode           = safeMode;
     this.dispatchCallReturn = dispatchCall;
     ValidateExecutionOnInit(true); //Forced to wait, the return-result should always be available after execution
 }
예제 #4
0
 /// <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)
 {
     return(MainThreadDispatcher.DispatchToMainThreadReturn(dispatchCall, safeMode));
 }
예제 #5
0
 public void Init(ThreadDispatchDelegateReturn dispatchCall, bool safeMode)
 {
     this.safeMode = safeMode;
     this.dispatchCallReturn = dispatchCall;
     ValidateExecutionOnInit(true); //Forced to wait, the return-result should always be available after execution
 }
예제 #6
0
 /// <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;
 }