我是否需要在单语言 UWP 应用程序中使用 create_async?

Do I ever need create_async within a single-language UWP app?

我想更好地理解托管 C++(如 C++/CX)中的 PPL 任务。我不清楚的一点是使用 create_task 与 create_async。 MS 文档说:

Use create_async only when you have to create functionality that can be accessed from another language or another Windows Runtime component. Use the task class directly when you know that the operation is both produced and consumed by C++ code in the same component."

因此,如果我仅在托管 C++ UWP 应用程序中编写异步函数,那么使用 create_async 毫无意义,我最好还是坚持使用 create_task?

请注意,C++/CX 不是 托管 C++。它们共享一些常见的语法扩展(如引用计数 "hat" 指针),但 C++/CX 是 100% 本机代码,并且在托管 CLR 运行 上没有 运行 时间。

也就是说,如果您想通过 public WinRT 接口公开异步操作,则需要使用 create_async - 无论接口的使用者是 C++、C#、JavaScript,或另一种语言。这是因为 create_async returns 一个 Windows::Foundation::IAsyncAction / IAsyncOperation<T>,这是 WinRT 支持的类型,可以被(例如)C# await 关键字使用。

如果您只是从其他 C++ 代码 直接 调用您的函数(即,不通过 public WinRT 接口),那么您不需要使用 create_async.

另请注意,create_async and create_task 做不同的事情 -- 前者 运行 是作为异步操作的仿函数,而 returns 是供其他 WinRT 组件使用的 IAsyncFoo,而后者接受来自另一个 WinRT 组件的 IAsyncXyz 值并在其周围包装一个 task 对象。