首页 > 解决方案 > 如何将带有取消的 C++/CX create_task 转换为等效的 C++/WinRT 代码?

问题描述

如何获取取消令牌源以将下面的 create_task C++/CX 调用替换为等效的 C++/WinRT 代码?

void MainPage::OnButtonClick(Object^ sender, RoutedEventArgs^ args)
{
    MessageDialog^ msgdlg = ref new MessageDialog("Choose a color", "How To Cancel Async");
    msgdlg->Commands->Append(ref new UICommand("Red", nullptr, Colors::Red));
    //...

    // Show the MessageDialog
    cancellationTokenSource = cancellation_token_source();
    task<IUICommand^> showTask = create_task(msgdlg->ShowAsync(),
        cancellationTokenSource.get_token());
    showTask.then([this, timer](task<IUICommand^> thisTask)
    {
        //...
    });
}

void MainPage::OnTimerTick(Object^ sender, Object^ args)
{
    cancellationTokenSource.cancel();
}

标签: c++-cxc++-winrt

解决方案


推荐阅读