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);
                        }
                    }
                }
            }
        }
예제 #2
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);
        }
예제 #3
0
        private static void CreateHelperGameObject()
        {
            GameObject            helperGO = new GameObject("UnityActivityHelper");
            UnityActivityWatchdog helper   = helperGO.AddComponent <UnityActivityWatchdog>();

            helperGO.hideFlags = helper.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;
            GameObject.DontDestroyOnLoad(helperGO);
            helperCreated = true;
        }
예제 #4
0
        public static void Init()
        {
            bool flag = !UnityActivityWatchdog.helperCreated;

            if (flag)
            {
                UnityActivityWatchdog.CreateHelperGameObject();
            }
        }
        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);
        }
예제 #6
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();
         }
     }
 }
예제 #7
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);
                }
            }
        }
예제 #8
0
        /// <summary>
        /// IThreadWorkerObject implementation: This method computes all the work.
        /// </summary>
        public void ExecuteThreadedWork()
        {
            if (workLoad == null || workLoad.Length == 0)
            {
                return;
            }

            //Debug.Log("Execute ID: " + ID + ", startIndex: " + startIndex  + ", endIndex: " + endIndex);
            if (workloadExecutor != null)
            {
                for (int i = startIndex; i < endIndex && !_isAborted; i++)
                {
                    UnityActivityWatchdog.SleepOrAbortIfUnityInactive();
                    workloadExecutor(workLoad[i]);
                }
            }
            else if (workloadExecutorIndexed != null)
            {
                for (int i = startIndex; i < endIndex && !_isAborted; i++)
                {
                    UnityActivityWatchdog.SleepOrAbortIfUnityInactive();
                    workloadExecutorIndexed(workLoad[i], i);
                }
            }
            else if (workloadExecutorArg != null)
            {
                for (int i = startIndex; i < endIndex && !_isAborted; i++)
                {
                    UnityActivityWatchdog.SleepOrAbortIfUnityInactive();
                    workloadExecutorArg(workLoad[i], extraArgument);
                }
            }
            else if (workloadExecutorArgIndexed != null)
            {
                for (int i = startIndex; i < endIndex && !_isAborted; i++)
                {
                    UnityActivityWatchdog.SleepOrAbortIfUnityInactive();
                    workloadExecutorArgIndexed(workLoad[i], i, extraArgument);
                }
            }
        }
        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);
                    }
                }
            }
        }
예제 #10
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);
        }
예제 #11
0
 private void Awake()
 {
     MainThreadWatchdog.Init();
     UnityActivityWatchdog.Init();
     InvokeRepeating("UpdateMainThreadDispatcher", WaitForSecondsTime, WaitForSecondsTime);
 }
예제 #12
0
 private void OnApplicationFocus(bool focus)
 {
     UnityActivityWatchdog.unityFocused = focus;
     UnityActivityWatchdog.UpdateStatus();
 }
예제 #13
0
 private void OnApplicationPause(bool pause)
 {
     UnityActivityWatchdog.unityPaused = pause;
     UnityActivityWatchdog.UpdateStatus();
 }
예제 #14
0
 private void OnApplicationQuit()
 {
     UnityActivityWatchdog.unityRunning = false;
     UnityActivityWatchdog.UpdateStatus();
 }
예제 #15
0
 public static bool CheckUnityActive()
 {
     UnityActivityWatchdog.Init();
     return(UnityActivityWatchdog.combinedActive);
 }
        public void ExecuteThreadedWork()
        {
            bool flag = this.workLoad == null || this.workLoad.Length == 0;

            if (!flag)
            {
                bool flag2 = this.workloadExecutor != null;
                if (flag2)
                {
                    int num = this.startIndex;
                    while (num < this.endIndex && !this._isAborted)
                    {
                        UnityActivityWatchdog.SleepOrAbortIfUnityInactive();
                        this.workloadExecutor(this.workLoad[num]);
                        int num2 = num;
                        num = num2 + 1;
                    }
                }
                else
                {
                    bool flag3 = this.workloadExecutorIndexed != null;
                    if (flag3)
                    {
                        int num3 = this.startIndex;
                        while (num3 < this.endIndex && !this._isAborted)
                        {
                            UnityActivityWatchdog.SleepOrAbortIfUnityInactive();
                            this.workloadExecutorIndexed(this.workLoad[num3], num3);
                            int num2 = num3;
                            num3 = num2 + 1;
                        }
                    }
                    else
                    {
                        bool flag4 = this.workloadExecutorArg != null;
                        if (flag4)
                        {
                            int num4 = this.startIndex;
                            while (num4 < this.endIndex && !this._isAborted)
                            {
                                UnityActivityWatchdog.SleepOrAbortIfUnityInactive();
                                this.workloadExecutorArg(this.workLoad[num4], this.extraArgument);
                                int num2 = num4;
                                num4 = num2 + 1;
                            }
                        }
                        else
                        {
                            bool flag5 = this.workloadExecutorArgIndexed != null;
                            if (flag5)
                            {
                                int num5 = this.startIndex;
                                while (num5 < this.endIndex && !this._isAborted)
                                {
                                    UnityActivityWatchdog.SleepOrAbortIfUnityInactive();
                                    this.workloadExecutorArgIndexed(this.workLoad[num5], num5, this.extraArgument);
                                    int num2 = num5;
                                    num5 = num2 + 1;
                                }
                            }
                        }
                    }
                }
            }
        }