Concurrency::task.wait() throws invalid_operation exception: "Illegal to wait on a task in a Windows Runtime STA."
This exception occurs since ~14th November 2022 and seems to be Microsoft update related.
The exception does not occur, when building in Debug mode. Edit: it was true only for some Visual Studio versions. The latest release do throw the exception regardless of Debug or Release modes.
The code runs in a C++ application as managed-C++.
Any known Microsoft issues in this direction? Edit: the exception seems to be correct but it never showed up until some updates.
The issue appears if single threaded
COMinitialization viaCoInitializeEx(NULL, COINIT_APARTMENTTHREADED);orCoInitialize(NULL);is done prior to callingConcurrency::task.wait().The issue can be fixed by initializing
COMwithCoInitializeEx(NULL, COINIT_MULTITHREADED);or
Windows::Foundation::Initialize(RO_INIT_MULTITHREADED);If you want to call
Concurrency::task.wait()on a thread which is already initialized asCOINIT_APARTMENTTHREADEDand you cannot change it - start your concurrency task in a new thread via e.g.std::thread.