Esempio n. 1
0
        /// <summary>
        /// Starts an method running on a new thread. The Thread dies when the method has stopped running.
        /// You can now make use of the DispatchToMainThread-actions & WaitForNextFrame
        /// </summary>
        /// <param name="targetMethod">The method that will be executed by the thread</param>
        /// <param name="argument">Object to pass to the targetMethod as soon as the Thread is started</param>
        /// <param name="priority">Thread priority</param>
        /// <returns>Newly instantiated Thread</returns>
        public static Thread StartSingleThread(ParameterizedThreadStart targetMethod, object argument, System.Threading.ThreadPriority priority = System.Threading.ThreadPriority.Normal, bool safeMode = true)
        {
            Init();
            MainThreadWatchdog.Init();
            MainThreadDispatcher.Init();
            UnityActivityWatchdog.Init();

            Thread result = null;

            if (safeMode)
            {
                SafeSingleThreadSession sessionData = new SafeSingleThreadSession(targetMethod);
                result = new Thread(sessionData.SafeExecte_ParamThreadStart);
            }
            else
            {
                result = new Thread(targetMethod);
            }

            result.Priority = priority;
            startedThreads.Add(result);

            result.Start(argument);
            return(result);
        }
        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);
                        }
                    }
                }
            }
        }
        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);
        }
Esempio n. 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);
            }
        }
        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);
        }
 /// <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);
     }
 }
Esempio n. 8
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);
     }
 }
Esempio n. 9
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);
     }
 }
Esempio n. 10
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();
         }
     }
 }
 private void ValidateExecutionOnInit(bool waitForExecution)
 {
     if (waitForExecution)
     {
         if (!MainThreadWatchdog.CheckIfMainThread())
         {
             while (!this.executed && Loom.CheckUnityActive())
             {
                 Thread.Sleep(5);
             }
         }
         else
         {
             this.ExecuteDispatch();
         }
     }
 }
        //--------------------------------------- 4 DIFFERENT OVERLOADS --------------------------------------
        //--------------------------------------- 4 DIFFERENT OVERLOADS --------------------------------------



        private void ValidateExecutionOnInit(bool waitForExecution)
        {
            if (waitForExecution)
            {
                if (!MainThreadWatchdog.CheckIfMainThread())
                {
                    while (!executed)
                    {
                        Thread.Sleep(1);
                    }
                }
                else
                {
                    ExecuteDispatch();
                }
            }
        }
Esempio n. 13
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();
         }
     }
 }
Esempio n. 14
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);
 }
Esempio n. 15
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);
 }
Esempio n. 16
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));
 }
Esempio n. 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);
            }
        }
Esempio n. 19
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);
                }
            }
        }
        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);
                    }
                }
            }
        }
Esempio n. 21
0
        public static Thread StartSingleThread(ParameterizedThreadStart targetMethod, object argument, System.Threading.ThreadPriority priority = System.Threading.ThreadPriority.Normal, bool safeMode = true)
        {
            SingleThreadStarter.Init();
            MainThreadWatchdog.Init();
            MainThreadDispatcher.Init();
            UnityActivityWatchdog.Init();
            Thread thread;

            if (safeMode)
            {
                SingleThreadStarter.SafeSingleThreadSession @object = new SingleThreadStarter.SafeSingleThreadSession(targetMethod);
                thread = new Thread(new ParameterizedThreadStart(@object.SafeExecte_ParamThreadStart));
            }
            else
            {
                thread = new Thread(targetMethod);
            }
            thread.Priority = priority;
            SingleThreadStarter.startedThreads.Add(thread);
            thread.Start(argument);
            return(thread);
        }
Esempio n. 22
0
 private void Awake()
 {
     MainThreadWatchdog.Init();
     DontDestroyOnLoad(this.gameObject);
     InvokeRepeating("UpdateMainThreadDispatcher", WaitForSecondsTime, WaitForSecondsTime);
 }
Esempio n. 23
0
 public static bool CheckIfMainThread()
 {
     MainThreadWatchdog.Init();
     return(Thread.CurrentThread == MainThreadWatchdog.mainThread);
 }
Esempio n. 24
0
 private void Awake()
 {
     MainThreadWatchdog.Init();
     UnityActivityWatchdog.Init();
     InvokeRepeating("UpdateMainThreadDispatcher", WaitForSecondsTime, WaitForSecondsTime);
 }