Example #1
0
        // that blocks ui-thread
        private async Task DoItWithAsyncNoAwaitButWaitWillBlockOnUiThread()
        {
            var info = new InfoObject {
                Logger = LogIt, MillisToSleep = 2000, TestCase = "AsyncNoAwaitWillBlockInUI-Thread"
            };
            var lenghtyStuff = new LengthyStuff();

            lenghtyStuff.DoItInAsync(info).Wait();
        }
Example #2
0
        private async Task DoItWithAsyncAndAwait()
        {
            var info = new InfoObject {
                Logger = LogIt, MillisToSleep = GetSimulatedWorkInMillis(), TestCase = "AsyncAndAwait"
            };
            var lenghtyStuff = new LengthyStuff();

            await lenghtyStuff.DoItInAsync(info);
        }
Example #3
0
        private Task DoItInSync()
        {
            var info = new InfoObject {
                Logger = LogIt, MillisToSleep = GetSimulatedWorkInMillis(), TestCase = "Sync"
            };
            var lenghtyStuff = new LengthyStuff();

            lenghtyStuff.DoItInSync(info);

            // in order to match delegate
            return(Task.Delay(0));
        }
Example #4
0
        static async Task DoItWithAsyncNoAwaitButConfigureAwaitFalse()
        {
            string currentMethodName = nameof(DoItWithAsyncNoAwaitButConfigureAwaitFalse);
            var    info = new InfoObject {
                MillisToSleep = 1333, TestCase = "AsyncNoAwaitButConfigureAwaitFalse"
            };
            var lenghtyStuff = new LengthyStuff();

            info.Log($"Start of <{currentMethodName}>");
            lenghtyStuff.TaskDelay(info, false).Wait();
            info.Log($"End of <{currentMethodName}>");
        }
Example #5
0
        static async Task DoItWithAsyncNoAwaitButWaitDoesNotBlockInConsole()
        {
            string currentMethodName = nameof(DoItWithAsyncNoAwaitButWaitDoesNotBlockInConsole);
            var    info = new InfoObject {
                MillisToSleep = 1246, TestCase = "AsyncNoAwaitNonBlockingInConsole"
            };
            var lenghtyStuff = new LengthyStuff();

            info.Log($"Start of <{currentMethodName}>");
            lenghtyStuff.DoItInAsync(info).Wait();
            info.Log($"End of <{currentMethodName}>");
        }
Example #6
0
        static async Task DoItWithAsyncAndAwait()
        {
            string currentMethodName = nameof(DoItWithAsyncAndAwait);
            var    info = new InfoObject {
                MillisToSleep = 1357, TestCase = "AsyncAndAwait"
            };
            var lenghtyStuff = new LengthyStuff();

            info.Log($"Start of <{currentMethodName}>");
            await lenghtyStuff.DoItInAsync(info);

            info.Log($"End of <{currentMethodName}>");
        }
Example #7
0
        static void DoItInSync()
        {
            string currentMethodName = nameof(DoItInSync);

            var info = new InfoObject {
                MillisToSleep = 1234, TestCase = "Sync"
            };
            var lenghtyStuff = new LengthyStuff();

            info.Log($"Start of <{currentMethodName}>");
            lenghtyStuff.DoItInSync(info);
            info.Log($"End of <{currentMethodName}>");
        }
Example #8
0
        static async Task DoItInAsyncInNewThread()
        {
            string currentMethodName = nameof(DoItInAsyncInNewThread);
            var    info = new InfoObject {
                MillisToSleep = 1377, TestCase = "AsyncThread"
            };
            var lenghtyStuff = new LengthyStuff();

            info.Log($"Start of <{currentMethodName}>");
            await lenghtyStuff.DoItInAsyncInNewThread(info);

            info.Log($"End of <{currentMethodName}>");
        }
Example #9
0
        private async Task DoItWithAsyncNoAwaitButConfigureAwaitFalse()
        {
            string currentMethodName = nameof(DoItWithAsyncNoAwaitButConfigureAwaitFalse);
            var    info = new InfoObject {
                Logger = LogIt, MillisToSleep = GetSimulatedWorkInMillis(), TestCase = "NoAwaitButConfigureAwaitFalse"
            };
            var lenghtyStuff = new LengthyStuff();

            info.IncreaseIndentationLevel();
            info.Log($"Start of <{currentMethodName}> before hard waiting on Task.Delay");
            lenghtyStuff.TaskDelay(info, false).Wait();
            info.Log($"End of <{currentMethodName}> back from Task.Delay().Wait()");
            info.DecreaseIndentationLevel();
        }
Example #10
0
        private async Task DoItInAsyncInNewThreadButConfigureAwaitFalse()
        {
            string currentMethodName = nameof(DoSyncCallViaTaskRunAndAsync);
            var    info = new InfoObject {
                Logger = LogIt, MillisToSleep = GetSimulatedWorkInMillis(), TestCase = "AsyncInNewThreadButConfigureAwaitFalse"
            };
            var lenghtyStuff = new LengthyStuff();

            info.IncreaseIndentationLevel();

            info.Log($"Begin of {currentMethodName} before Task.Delay");
            var task = Task.Run(() => lenghtyStuff.TaskDelay(info, false));

            info.Log($"In {currentMethodName} before awaiting Task-Delay");
            await task;

            info.Log($"End of {currentMethodName} after awaiting Task.Delay");
            info.DecreaseIndentationLevel();
        }
Example #11
0
        private async Task DoSyncCallViaTaskRunAndAsync()
        {
            string currentMethodName = nameof(DoSyncCallViaTaskRunAndAsync);
            var    info = new InfoObject {
                Logger = LogIt, MillisToSleep = GetSimulatedWorkInMillis(), TestCase = "AsyncViaTaskRun"
            };
            var lenghtyStuff = new LengthyStuff();

            info.IncreaseIndentationLevel();

            info.Log($"Begin of {currentMethodName} before calling DoItInSync");
            var task = Task.Run(() => lenghtyStuff.DoItInSync(info));

            info.Log($"In {currentMethodName} before awaiting DoItInSync");
            await task;

            info.Log($"End of {currentMethodName} after awaiting DoItInSync");

            info.DecreaseIndentationLevel();
        }
Example #12
0
        static async Task HandleLongRunningTask()
        {
            string      currentMethodName = nameof(HandleLongRunningTask);
            Task <bool> task   = null;
            bool        result = false;
            var         _cts   = new CancellationTokenSource();
            var         info   = new InfoObject {
                ThrowIfCancellingRequesting = true, TestCase = "CancellationToken", MillisToSleep = 2112
            };
            var lenghtyStuff = new LengthyStuff();

            info.IncreaseIndentationLevel();

            // cancel the task after some time
            _cts.CancelAfter(4567);

            try
            {
                info.Log($"In {currentMethodName} before starting DoLengthy...");
                // there is really not much difference
                //task = lenghtyStuff.DoLengthyOperationAsyncWithCancellationToken(info, _cts.Token);
                //task = Task.Run(async () => await lenghtyStuff.DoLengthyOperationAsyncWithCancellationToken(info, _cts.Token), _cts.Token);
                task = lenghtyStuff.DoLengthyOpAsyncWithCtInNewThread(info, _cts.Token);

                result = await task;
                info.Log($"In {currentMethodName} after awaiting DoLengthy...");
            }
            catch (OperationCanceledException)
            {
                info.Log($"In {currentMethodName} received OperationCanceledException, return false");
                result = false;
            }

            info.Log($"In {currentMethodName} Task.State <{task?.Status}>, Result: <{result}>");
            info.DecreaseIndentationLevel();
        }
Example #13
0
        private async Task <bool> HandleLongRunningTask()
        {
            string      currentMethodName = nameof(HandleLongRunningTask);
            Task <bool> task   = null;
            bool        result = false;

            _cts = new CancellationTokenSource();
            var info = new InfoObject {
                Logger = LogIt, ThrowIfCancellingRequesting = true, TestCase = "CancellationToken", MillisToSleep = GetSimulatedWorkInMillis()
            };
            var lenghtyStuff = new LengthyStuff();

            info.IncreaseIndentationLevel();

            int testVariant = GetTestVariant();

            if (testVariant == TV_LONG_RUNNING_CANCELED_BEFORE_START)
            {
                // cancel the task right away, will throw TaskCanceledException, if ct is provided for Task and not only for Method
                _cts.Cancel();
            }

            try
            {
                info.Log($"In {currentMethodName} before starting DoLengthy...");
                if (testVariant == TV_LONG_RUNNING_JUST_AWAIT)
                {
                    task = lenghtyStuff.DoLengthyOperationAsyncWithCancellationToken(info, _cts.Token);
                }
                else if (testVariant == TV_LONG_RUNNING_AWAIT_AND_TASK_RUN)
                {
                    task = Task.Run(async() => await lenghtyStuff.DoLengthyOperationAsyncWithCancellationToken(info, _cts.Token), _cts.Token);
                }
                else if (testVariant == TV_LONG_RUNNING_AWAIT_AND_TASK_RUN_WITH_EXCEPTION_HANDLING)
                {
                    task = lenghtyStuff.DoLengthyOpAsyncWithCtInNewThread(info, _cts.Token);
                }

                if (null == task)
                {
                    // to see a status of a task
                    task = Task.Run(async() => { await Task.Delay(1); return(false); }, _cts.Token);
                }

                result = await task;
                info.Log($"In {currentMethodName} after awaiting DoLengthy...");
            }
            // unnecessary, cos TaskCanceledException is derived from OperationCanceledExceoption
            //catch (TaskCanceledException)
            //{
            //    info.Log($"In {currentMethodName} received TaskCanceledException, return false");
            //    result = false;
            //}
            catch (OperationCanceledException)
            {
                info.Log($"In {currentMethodName} received OperationCanceledException, return false");
                result = false;
            }

            info.Log($"In {currentMethodName} Task.State <{task?.Status}>, Result: <{result}>");
            info.DecreaseIndentationLevel();
            return(result);
        }