问题描述
调用create_task时,是否有办法确保该任务不在UI线程上运行?
When calling create_task is there a way to ensure that the task doesn't run on the UI thread?
我想确保我不会在某种程度上设法在UI线程上执行的任务中意外调用wait.
I want to be sure I'm not inadvertently calling wait inside a task that somehow managed to execute on the UI thread.
推荐答案
create_task函数不会自发跳转到UI线程:如果不从UI线程调用它,它将无法在其中执行.您需要显式调用Dispatcher才能返回.
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.
默认情况下,如果未提供task_continuation_context来告知公寓的任务(返回IAsyncAction或IAsyncOperation的任务)将继续在其公寓中继续进行.在UI线程上启动任务的常见情况将在UI线程上继续.
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.
请参阅MSDN 异步的管理线程上下文"部分有关更多详细信息,请参见C ++ 文档中的编程.
See the Managing the thread context section of MSDN's Asynchronous programming in C++ docs for more details.
这篇关于PPL任务何时在UI线程上执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!