microsoft / cppwinrt

C++/WinRT
MIT License
1.64k stars 236 forks source link

Feature request: Overriding "resume in same apartment" behavior for `co_await IAsyncXxx` #1355

Closed oldnewthing closed 11 months ago

oldnewthing commented 11 months ago

Version

2.0.230706.1

Summary

By default co_await IAsyncXxx resumes in the same COM apartment. Sometimes, this is not desirable behavior. In C#, you can use ConfigureAwait(false) to disable the resume-in-same-apartment behavior. C++/WinRT would benefit from having a corresponding capability.

A wil-based version was implemented in microsoft/wil#325 but @kennykerr allowd this to be added to core C++/WinRT since it greatly simplifies the implementation, and prevents it from falling out of sync.

Reproducible example

#include <winrt/Windows.Foundation.h>

using namespace winrt;
using namespace winrt::Windows::Foundation;

extern IAsyncAction Something();

IAsyncAction Example()
{
    // This co_await resumes in the same apartment.
    co_await Something();

    // This co_await is permitted to resume in any apartment.
    co_await winrt::resume_agile(Something());
}

Additional comments

The name resume_agile is up for debate, as is the preference for a free function or a member function. See the PR for details.