0

我读样品希洛由微软提供,ImageBrowserViewModel.cpp下有一些代码,我不明白:线程在Windows商店应用

// 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)); 

的追求是使用应用IsBackgroundThread() & IsMainThread()断言它应该是在特定环境下正确调用。但对于::wait(timeToWait)函数调用,没有task_continuation_context定义来确保它在后台运行,我只是想知道它是如何工作的?非常感谢!

回答

1

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