public static T DispatchToMainThreadReturn <T>(CallBackReturn <T> dispatchCall, bool safeMode = true)
        {
            bool flag = MainThreadWatchdog.CheckIfMainThread();
            T    result;

            if (flag)
            {
                bool flag2 = dispatchCall != null;
                if (flag2)
                {
                    result = dispatchCall();
                }
                else
                {
                    result = default(T);
                }
            }
            else
            {
                ThreadDispatchAction <T> threadDispatchAction = new ThreadDispatchAction <T>();
                MainThreadDispatcher.dispatchActions.Enqueue(threadDispatchAction);
                threadDispatchAction.Init(dispatchCall, safeMode);
                result = threadDispatchAction.dispatchExecutionResult2;
            }
            return(result);
        }
        public static object DispatchToMainThreadReturn <T>(CallBackArgRturn <T> dispatchCall, T dispatchArgument, bool safeMode = true)
        {
            bool   flag = MainThreadWatchdog.CheckIfMainThread();
            object result;

            if (flag)
            {
                bool flag2 = dispatchCall != null;
                if (flag2)
                {
                    result = dispatchCall(dispatchArgument);
                }
                else
                {
                    result = null;
                }
            }
            else
            {
                ThreadDispatchAction <T> threadDispatchAction = new ThreadDispatchAction <T>();
                MainThreadDispatcher.dispatchActions.Enqueue(threadDispatchAction);
                threadDispatchAction.Init(dispatchCall, dispatchArgument, safeMode);
                result = threadDispatchAction.dispatchExecutionResult;
            }
            return(result);
        }
コード例 #3
0
        public ThreadWaitForNextFrame(int waitFrames = 1, int sleepTime = 5)
        {
            bool flag = waitFrames > 0;

            if (flag)
            {
                bool flag2 = MainThreadWatchdog.CheckIfMainThread();
                if (flag2)
                {
                    Debug.Log("Its not allowed to put the MainThread to sleep!");
                }
                else
                {
                    int  currentFrame = MainThreadDispatcher.currentFrame;
                    bool flag3        = !UnityActivityWatchdog.CheckUnityRunning();
                    if (!flag3)
                    {
                        Thread.Sleep(sleepTime);
                        while (!UnityActivityWatchdog.CheckUnityActive() || currentFrame + waitFrames >= MainThreadDispatcher.currentFrame)
                        {
                            Thread.Sleep(sleepTime);
                        }
                    }
                }
            }
        }
コード例 #4
0
        public ThreadWaitForSeconds(float seconds)
        {
            if (MainThreadWatchdog.CheckIfMainThread())
            {
                Debug.Log("Its not allowed to put the MainThread to sleep!");
                return;
            }
			
			if(!UnityActivityWatchdog.CheckUnityRunning())
				return;

            Thread.Sleep((int)Mathf.Max( 1, Mathf.Round(seconds * 1000f)));
            while (!UnityActivityWatchdog.CheckUnityActive())
                Thread.Sleep(5);
        }
コード例 #5
0
        public ThreadWaitForSeconds(float seconds)
        {
            if (MainThreadWatchdog.CheckIfMainThread())
            {
                Debug.Log("Its not allowed to put the MainThread to sleep!");
                return;
            }

            Thread.Sleep((int)Mathf.Max(1, Mathf.Round(seconds * 1000f)));

            while (!MainThreadDispatcher.gameEnded && !MainThreadDispatcher.gameActive)
            {
                Thread.Sleep(5);
            }
        }
コード例 #6
0
 public static void DispatchToMainThread <T>(CallBackArg <T> dispatchCall, T dispatchArgument, bool waitForExecution = false, bool safeMode = true)
 {
     if (MainThreadWatchdog.CheckIfMainThread())
     {
         if (dispatchCall != null)
         {
             dispatchCall(dispatchArgument);
         }
     }
     else
     {
         ThreadDispatchAction <T> t = new ThreadDispatchAction <T>();
         dispatchActions.Enqueue(t);
         t.Init(dispatchCall, dispatchArgument, waitForExecution, safeMode);
     }
 }
コード例 #7
0
 /// <summary>
 /// When executed by the MainThread, this argument will be passed to the "dispatchCall";
 /// </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="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(ThreadDispatchDelegateArg dispatchCall, object dispatchArgument, bool waitForExecution = false, bool safeMode = true)
 {
     if (MainThreadWatchdog.CheckIfMainThread())
     {
         if (dispatchCall != null)
         {
             dispatchCall(dispatchArgument);
         }
     }
     else
     {
         ThreadDispatchAction action = new ThreadDispatchAction();
         lock (dispatchActions) { dispatchActions.Add(action); }
         action.Init(dispatchCall, dispatchArgument, waitForExecution, safeMode);
     }
 }
コード例 #8
0
 public static void DispatchToMainThread(CallBack dispatchCall, bool waitForExecution = false, bool safeMode = true)
 {
     if (MainThreadWatchdog.CheckIfMainThread())
     {
         if (dispatchCall != null)
         {
             dispatchCall();
         }
     }
     else
     {
         ThreadDispatchAction <object> t = new ThreadDispatchAction <object>();
         dispatchActions.Enqueue(t);
         t.Init(dispatchCall, waitForExecution, safeMode);
     }
 }
コード例 #9
0
 /// <summary>
 /// !IMPORTANT! This method should be called regularly within routines that take more then half a second to complete, to make sure IOS for example is able to force an application to sleep when it looses focus or gets puched to the background.
 /// This is a very light-weight check, internally it only needs to check two static booleans once everything is Initialized and running.
 /// You can use this without causing any serious overhead.
 /// Motivation: Sins Threads cannot be put asleep from the outside, it needs the be managed from within the thread itself, thats why this method was build.
 ///
 /// Example:
 /// for(int i = 0; i < 999999999; i++)
 /// {
 ///     Loom.SleepOrAbortIfUnityInactive(); //Prevents IOS for example of killing this app because the threads won't sleep once your unity-app is puched to the background.
 ///     //Do something heavy that will cause this routine to run more then 0.5 seconds.
 /// }
 /// </summary>
 public static void SleepOrAbortIfUnityInactive()
 {
     Init();
     while (!combinedActive && !MainThreadWatchdog.CheckIfMainThread())
     {
         if (unityRunning)
         {
             //Debug.Log("UNITY INACTIVE: About to sleep for 100 ms: " + Thread.CurrentThread.ManagedThreadId);
             Thread.Sleep(100);
         }
         else
         {
             //Debug.Log("UNITY NOT RUNNING: About to abort: " + Thread.CurrentThread.ManagedThreadId);
             Thread.CurrentThread.Abort();
         }
     }
 }
コード例 #10
0
        //--------------------------------------- 4 DIFFERENT OVERLOADS --------------------------------------
        //--------------------------------------- 4 DIFFERENT OVERLOADS --------------------------------------



        private void ValidateExecutionOnInit(bool waitForExecution)
        {
            if (waitForExecution)
            {
                if (!MainThreadWatchdog.CheckIfMainThread())
                {
                    while (!executed)
                    {
                        Thread.Sleep(1);
                    }
                }
                else
                {
                    ExecuteDispatch();
                }
            }
        }
コード例 #11
0
 public static void SleepOrAbortIfUnityInactive()
 {
     UnityActivityWatchdog.Init();
     while (!UnityActivityWatchdog.combinedActive && !MainThreadWatchdog.CheckIfMainThread())
     {
         bool flag = UnityActivityWatchdog.unityRunning;
         if (flag)
         {
             Thread.Sleep(100);
         }
         else
         {
             Thread.CurrentThread.Interrupt();
             Thread.CurrentThread.Join();
         }
     }
 }
 private void ValidateExecutionOnInit(bool waitForExecution)
 {
     if (waitForExecution)
     {
         if (!MainThreadWatchdog.CheckIfMainThread())
         {
             while (!this.executed && Loom.CheckUnityActive())
             {
                 Thread.Sleep(5);
             }
         }
         else
         {
             this.ExecuteDispatch();
         }
     }
 }
コード例 #13
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);
 }
コード例 #14
0
 public static T DispatchToMainThreadReturn <T>(CallBackReturn <T> dispatchCall, bool safeMode = true)
 {
     if (MainThreadWatchdog.CheckIfMainThread())
     {
         if (dispatchCall != null)
         {
             return(dispatchCall());
         }
     }
     else
     {
         ThreadDispatchAction <T> t = new ThreadDispatchAction <T>();
         dispatchActions.Enqueue(t);
         t.Init(dispatchCall, safeMode);
         return(t.dispatchExecutionResult2);
     }
     return(default(T));
 }
コード例 #15
0
 public static object DispatchToMainThreadReturn <T>(CallBackArgRturn <T> dispatchCall, T dispatchArgument, bool safeMode = true)
 {
     if (MainThreadWatchdog.CheckIfMainThread())
     {
         if (dispatchCall != null)
         {
             return(dispatchCall(dispatchArgument));
         }
     }
     else
     {
         ThreadDispatchAction <T> t = new ThreadDispatchAction <T>();
         dispatchActions.Enqueue(t);
         t.Init(dispatchCall, dispatchArgument, safeMode);
         return(t.dispatchExecutionResult);
     }
     return(null);
 }
コード例 #16
0
        public ThreadWaitForNextFrame(int waitFrames = 1, int sleepTime = 5)
        {
            if (waitFrames > 0)
            {
                if (MainThreadWatchdog.CheckIfMainThread())
                {
                    Debug.Log("Its not allowed to put the MainThread to sleep!");
                    return;
                }

                int startFrame = MainThreadDispatcher.currentFrame;
                Thread.Sleep(sleepTime);

                while (!UnityActivityWatchdog.CheckUnityActive() || startFrame + waitFrames >= MainThreadDispatcher.currentFrame)
                {
                    Thread.Sleep(sleepTime);
                }
            }
        }
コード例 #17
0
        public ThreadWaitForNextFrame(int waitFrames = 1)
        {
            if (waitFrames > 0)
            {
                if (MainThreadWatchdog.CheckIfMainThread())
                {
                    Debug.Log("Its not allowed to put the MainThread to sleep!");
                    return;
                }

                int startFrame = MainThreadDispatcher.currentFrame;
                Thread.Sleep(5);

                while ((!MainThreadDispatcher.gameEnded && !MainThreadDispatcher.gameActive) || startFrame + waitFrames >= MainThreadDispatcher.currentFrame)
                {
                    Thread.Sleep(5);
                }
            }
        }
        public static void DispatchToMainThread <T>(CallBackArg <T> dispatchCall, T dispatchArgument, bool waitForExecution = false, bool safeMode = true)
        {
            bool flag = MainThreadWatchdog.CheckIfMainThread();

            if (flag)
            {
                bool flag2 = dispatchCall != null;
                if (flag2)
                {
                    dispatchCall(dispatchArgument);
                }
            }
            else
            {
                ThreadDispatchAction <T> threadDispatchAction = new ThreadDispatchAction <T>();
                MainThreadDispatcher.dispatchActions.Enqueue(threadDispatchAction);
                threadDispatchAction.Init(dispatchCall, dispatchArgument, waitForExecution, safeMode);
            }
        }
コード例 #19
0
        public ThreadWaitForSeconds(float seconds)
        {
            bool flag = MainThreadWatchdog.CheckIfMainThread();

            if (flag)
            {
                Debug.Log("Its not allowed to put the MainThread to sleep!");
            }
            else
            {
                bool flag2 = !UnityActivityWatchdog.CheckUnityRunning();
                if (!flag2)
                {
                    Thread.Sleep((int)Mathf.Max(1f, Mathf.Round(seconds * 1000f)));
                    while (!UnityActivityWatchdog.CheckUnityActive())
                    {
                        Thread.Sleep(5);
                    }
                }
            }
        }