【问题标题】:threading in Windows store appWindows 商店应用程序中的线程
【发布时间】:2013-07-21 23:12:20
【问题描述】:

我正在阅读 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));

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

【问题讨论】:

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


    【解决方案1】:

    构造任务的默认值(因为第一个任务在您的代码 sn-p 中)是task_continuation_context::use_arbitrary(),因此即使未指定它也是如此。任务延续 lambda 在调用 ::wait 之前确实断言了这一点(如果它试图在 UI 线程上运行,则会抛出异常)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多