Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if a task completed synchronously?

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?

like image 323
Water Cooler v2 Avatar asked Jun 04 '16 20:06

Water Cooler v2


People also ask

Does Task run run synchronously?

Even though the task runs synchronously, the calling thread should still call Wait to handle any exceptions that the task might throw.

How can I tell if a task is completed?

Show completed tasks in the Tasks viewsIn the Navigation Pane, click Tasks. In the Navigation Pane, click Completed Tasks.

Is Task run async or sync?

NET, Task. Run is used to asynchronously execute CPU-bound code.

Does await run synchronously?

Top-level code, up to and including the first await expression (if there is one), is run synchronously.


1 Answers

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.

like image 199
Zverev Evgeniy Avatar answered Sep 18 '22 12:09

Zverev Evgeniy