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