Windows商店应用中的线程

时间:2013-07-23 02:58:11

标签: multithreading windows-8 microsoft-metro windows-store-apps c++-cx

我正在阅读MS提供的示例Hilo,在ImageBrowserViewModel.cpp下有一些我不明白的代码:

// Observe the update after waiting the specified amount of time.
create_task([timeToWait]() {
    assert(IsBackgroundThread());
    ::wait(timeToWait);
}).then([weakThis]() {
    assert(IsMainThread());
    auto vm = weakThis.Resolve<ImageBrowserViewModel>();
    if (nullptr != vm)
    {
        vm->ObserveFileChange();
        vm->m_hasFileUpdateTask = false;
    }
}, task_continuation_context::use_current()).then(ObserveException<void>(m_exceptionPolicy));

任务是app app IsBackgroundThread()&amp; IsMainThread()断言它应该在某些上下文中正确调用。但是对于::wait(timeToWait)函数调用,没有定义task_continuation_context以确保它在后台运行,我只是想知道它是如何工作的?非常感谢!

1 个答案:

答案 0 :(得分:1)

构造任务的默认值(因为第一个任务在你的代码片段中)是task_continuation_context::use_arbitrary(),所以即使没有指定它也是如此。任务延续lambda在调用:: wait之前会断言它(如果它试图在UI线程上运行会抛出异常)。

相关问题