How to know if a task completed synchronously?

血红的双手。 提交于 2019-12-22 06:32:50

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/37634857/how-to-know-if-a-task-completed-synchronously

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!