void BackgroundToBackgroundException()
        {
            var task1 = UnityTask.Run(() =>
            {
                Debug.Log("1 Go");

                var task2 = UnityTask.Run(() =>
                {
                    UnityTask.Delay(100);
                    Debug.Log("2 Go");
                    throw new Exception("2 Fail");
                });

                task2.Wait();

                if (task2.IsFaulted)
                {
                    throw task2.Exception;
                }
            });

            task1.Wait();

            Debug.Log(task1.Status + " " + task1.Exception.Message);
        }
 void Background()
 {
     UnityTask.Run(() =>
     {
         Debug.Log("Sleeping...");
         UnityTask.Delay(2000);
         Debug.Log("Slept");
     });
 }
 void MainTest()
 {
     UnityTask.RunOnMain(() =>
     {
         Debug.Log("Sleeping...");
         UnityTask.Delay(2000);
         Debug.Log("Slept");
     });
 }
    private void RealWork()
    {
        const int count = 5;

        for (var y = 0; y < count; y++)
        {
            UnityTask.Delay(1000);
            Debug.Log("Delay " + y);
        }
    }
        void BackgroundToRotine()
        {
            UnityTask.Run(() =>
            {
                Debug.Log("Thread A Running");

                var task = UnityTask.RunCoroutine(RoutineFunction());

                while (task.IsRunning)
                {
                    Debug.Log(".");
                    UnityTask.Delay(500);
                }

                Debug.Log("Thread A Done");
            });
        }
        void BackgroundToMain()
        {
            UnityTask.Run(() =>
            {
                Debug.Log("Thread A Running");

                var task = UnityTask.RunOnMain(() =>
                {
                    Debug.Log("Sleeping...");
                    UnityTask.Delay(2000);
                    Debug.Log("Slept");
                });

                while (task.IsRunning)
                {
                    Debug.Log(".");
                    UnityTask.Delay(100);
                }

                Debug.Log("Thread A Done");
            });
        }
        public IEnumerator Start()
        {
            log = string.Empty;
            Debug.Log("Test Starting");
            Counter = 0;
            yield return(new WaitForSeconds(2));

            // Action
            Debug.Log("Action...");
            var mtask = UnityTask.Run(() => { Counter++; });

            yield return(mtask);

            Assert(() => Counter == 1, "Action");
            yield return(new WaitForSeconds(2));

            // Action with
            Debug.Log("Action w/ Continue...");
            yield return(new WaitForSeconds(1));

            yield return(UnityTask.Run(() =>
            {
                Counter++;
            }).ContinueWith(task =>
            {
                Counter++;
            }));

            Assert(() => Counter == 3, "Action w/ Continue");

            // Coroutine
            Debug.Log("Continue...");
            yield return(new WaitForSeconds(1));

            yield return(UnityTask.RunCoroutine(DemoCoroutine));

            Assert(() => Counter == 4, "Coroutine");

            // Coroutine with Continue
            Debug.Log("Coroutine w/ Continue...");
            yield return(new WaitForSeconds(1));

            yield return(UnityTask.RunCoroutine(DemoCoroutine).ContinueWith(task => { Counter++; }));

            Assert(() => Counter == 6, "Coroutine w/ Continue");

            // Coroutine with result
            Debug.Log("Coroutine w/ result...");
            yield return(new WaitForSeconds(1));

            var ctask = UnityTask.RunCoroutine <string>(DemoCoroutineWithResult);

            yield return(ctask);

            Assert(() => Counter == 7, "Coroutine w/ result (A)");
            Assert(() => ctask.Result == "Hello", "Coroutine w/ result (B)");


            // Coroutine with result and Continue
            Debug.Log("CoroutineCoroutine w/ result and continue...");
            yield return(new WaitForSeconds(1));

            var ctask2 = UnityTask.RunCoroutine <string>(DemoCoroutineWithResult).ContinueWith(t =>
            {
                Assert(() => t.Result == "Hello", "Coroutine w/ result and continue (A)");
                t.Result = "Goodbye";
                Counter++;
            });

            yield return(ctask2);

            Assert(() => Counter == 9, "Coroutine w/ result and continue (B)");
            Assert(() => ctask2.Result == "Goodbye", "Coroutine w/ result and continue (C)");


            // Function
            Debug.Log("Function...");
            yield return(new WaitForSeconds(1));

            var ftask = UnityTask.Run(() =>
            {
                Counter++;
                return("Hello");
            });

            yield return(ftask);

            Assert(() => Counter == 10, "Function");
            Assert(() => ftask.Result == "Hello", "Function");

            //Exception
            Debug.Log("Exception...");
            yield return(new WaitForSeconds(1));

            var etask = UnityTask.Run(() => { Counter++; throw new Exception("Hello"); });

            yield return(etask);

            Assert(() => Counter == 11, "Exception");
            Assert(() => etask.IsFaulted && etask.Exception.Message == "Hello", "Exception");

            //Main Thread
            log = string.Empty;
            Debug.Log("Basic Tests Passed");
            Debug.Log("Threading Tests...");
            Debug.Log("Background....");
            yield return(new WaitForSeconds(1));

            yield return(UnityTask.Run(() =>
            {
                UnityTask.Delay(50);
                Debug.Log("Sleeping...");
                UnityTask.Delay(2000);
                Debug.Log("Slept");
            }));

            yield return(1);

            Debug.Log("BackgroundToMain");
            yield return(new WaitForSeconds(1));

            yield return(UnityTask.Run(() =>
            {
                UnityTask.Delay(100);

                var task = UnityTask.RunOnMain(() =>
                {
                    Debug.Log("Hello From Main");
                });

                while (task.IsRunning)
                {
                    log += ".";
                    UnityTask.Delay(100);
                }
            }));

            yield return(1);

            Debug.Log("BackgroundToRotine");
            yield return(new WaitForSeconds(1));

            yield return(UnityTask.Run(() =>
            {
                var task = UnityTask.RunCoroutine(LongCoroutine());

                while (task.IsRunning)
                {
                    log += ".";
                    UnityTask.Delay(500);
                }
            }));

            yield return(1);

            Debug.Log("BackgroundToBackground");
            yield return(new WaitForSeconds(1));

            yield return(UnityTask.Run(() =>
            {
                Debug.Log("1 Sleeping...");

                UnityTask.Run(() =>
                {
                    Debug.Log("2 Sleeping...");
                    UnityTask.Delay(2000);
                    Debug.Log("2 Slept");
                });
                UnityTask.Delay(2000);
                Debug.Log("1 Slept");
            }));

            yield return(1);

            Debug.Log("Success All");
        }