问题
When calling create_task is there a way to ensure that the task doesn't run on the UI thread?
I want to be sure I'm not inadvertently calling wait inside a task that somehow managed to execute on the UI thread.
回答1:
The create_task function won't spontaneously jump onto the UI thread: if you don't call it from the UI thread it won't execute there. You need to explicitly call the Dispatcher to get back.
An apartment aware task (one which returns IAsyncAction or IAsyncOperation) will continue in its apartment by default if a task_continuation_context isn't provided to tell it otherwise. The common case of starting a task on a UI thread will continue on the UI thread.
See the Managing the thread context section of MSDN's Asynchronous programming in C++ docs for more details.
来源:https://stackoverflow.com/questions/30134704/when-does-a-ppl-task-execute-on-the-ui-thread