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(); }
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(); }
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(); }
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(); }
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); }