private static void StopRoutine(IEnumerator _routine)
 {
     if (_routine != null)
     {
         i.StopCoroutine(_routine);
     }
 }
Exemple #2
0
        private static IEnumerator PingRoutine(string url, Action <bool> callback)
        {
            Stopwatch watch = new Stopwatch();

            watch.Start();
            Ping ping = new Ping(url);

            Log.Debug("Ping started");

            bool shouldSkip = false;

            CoroutineController.DoAfterGivenTime(PingTimeOutDuration / 1000f, () => shouldSkip = true, "PingSkipTimer");

            yield return(new WaitUntil(() => ping.isDone || shouldSkip));


            watch.Stop();

            if (ping.isDone)
            {
                Log.Debug("Ping completed.");
                Log.Debug("Ping time: " + ping.time);

                // If pinging exceeds the default time (timeout), Unity returns -1 as ping.time
                // This ping.time < 0 check is for that reason
                if (ping.time < 0 && ping.time > PingTimeOutDuration)
                {
                    // ----FAIL----
                    Log.Error("Rateus Timeout. ping.time:" + ping.time);
                    callback.Invoke(false);

                    if (CoroutineController.IsCoroutineRunning("PingSkipTimer"))
                    {
                        CoroutineController.StopCoroutine("PingSkipTimer");
                    }
                }
                else
                {
                    // ----SUCCESS-----
                    callback.Invoke(true);
                }
            }
            else if (shouldSkip)
            {
                // ----FAIL----
                Log.Error("Ping failed.");
                callback.Invoke(false);
            }
            else
            {
                // ----FAIL----
                Log.Error("Unknown state.");
                callback.Invoke(false);
            }

            Log.Debug("Watch time: " + watch.Elapsed.TotalMilliseconds);
        }
        public void Start()
        {
            //Start Coroutine WITHOUT key
            RoutineWithoutKey().StartCoroutine();
            //CoroutineController.StartCoroutine(RoutineWithKey()); <--- This also can be used

            //Do After Given Time with ANONYMOUS action
            CoroutineController.DoAfterGivenTime(3f, () =>
            {
                Debug.Log("OVERRIDE!");
                //Override Coroutine WITHOUT key
                RoutineWithoutKey().StartCoroutine(true);

                CoroutineController.DoAfterGivenTime(3f, () =>
                {
                    //Stop Coroutine WITHOUT key
                    Debug.Log("STOP!");
                    RoutineWithoutKey().StopCoroutine();
                });
            });

            //Cancellable Do After
            CoroutineController.DoAfterGivenTime(10f, () =>
            {
                Debug.LogError("This should NOT be shown!");
            }, "killme");

            CoroutineController.DoAfterGivenTime(9f, () =>
            {
                CoroutineController.StopCoroutine("killme");
            });

            //Do After Given Time with action
            Action action = () =>
            {
                //Start Coroutine WITH key
                RoutineWithKey().StartCoroutine("myKey");
                CoroutineController.DoAfterGivenTime(3f, () =>
                {
                    //Stop Coroutine WITH KEY
                    RoutineWithKey().StopCoroutine("myKey");
                });
            };

            action.DoAfterGivenTime(12f);
            CoroutineController.DoAfterGivenTime(18f, () =>
            {
                RoutineWillDieItSelf().StartCoroutine(onFinished: isStopped =>
                {
                    Debug.Log("RoutineWillDieItSelf died by itself. IsStopped: " + isStopped);
                });
            });
        }