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.
CodePudding user response:
I've also only recently encountered this error without any known changes to the code.
my issue involves the Microsoft C Rest SDK. specifically a call like
try {
response = client.request(request).get();
} catch (...) {
}
CodePudding user response:
The issue appears if single threaded COM
initialization via CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
or CoInitialize(NULL);
is done prior to calling Concurrency::task.wait()
.
The issue can be fixed by initializing COM
with
CoInitializeEx(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 as COINIT_APARTMENTTHREADED
and you cannot change it - start your concurrency task in a new thread via e.g. std::thread
.