I faintly remember there being a property that tells us whether a task executed synchronously but my memory fails me just now. Or may be there isn't and I am mixing up the IAsyncResult.CompletedSynchronously
with this one?
Even though the task runs synchronously, the calling thread should still call Wait to handle any exceptions that the task might throw.
Show completed tasks in the Tasks viewsIn the Navigation Pane, click Tasks. In the Navigation Pane, click Completed Tasks.
NET, Task. Run is used to asynchronously execute CPU-bound code.
Top-level code, up to and including the first await expression (if there is one), is run synchronously.
Check this: Task.IAsyncResult.CompletedSynchronously Property
It's description looks much like the answer:
Gets an indication of whether the operation completed synchronously.
In order to check the property value you will have to cast the Task
into IAsyncResult
because the Task
implements it explicitly.
bool? result = (myTask as IAsyncResult)?.CompletedSynchronously;
Instead of the explicit cast you can use the extension method: WindowsRuntimeSystemExtensions.AsAsyncOperation defined in the System
namespace:
bool result = myTask.AsAsyncOperation().CompletedSynchronously;
As reasonably pointed out in the comment though there is a gag in the current property implementation. As of the first of October 2016 it always returns false
. This is a subject to change.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With