警告 C4451:使用 ref class BackgroundTaskDeferral 可能导致无效封送处理

Warning C4451: Usage of ref class BackgroundTaskDeferral can lead to invalid marshaling

关于我的 universal app project,我收到编译警告:

警告 C4451:'BackgroundServerTasks::ServerTask::Run::::deferral':在此上下文中使用 ref class 'Windows::ApplicationModel::Background::BackgroundTaskDeferral' 会导致跨上下文对象的无效封送处理

注意:考虑使用 'Platform::Agile' 代替

this file. How could I resolve this warning? Apparently, it is due to my using the BackgroundTaskDeferral within a create_task. I found no solution on Bing except for this post 中解决了这个问题,但我不确定它是否适用于这种情况。

令我惊讶的是 Windows::ApplicationModel::Background::BackgroundTaskDeferral 并不敏捷。

尝试在相同的上下文中完成延期,即:

create_task(writer->StoreAsync()).then([this, deferral](unsigned int n)
{
    deferral->Complete();
}, task_continuation_context::use_current());

另一种选择是使用 Platform::Agile<> 包装器:

Platform::Agile<Windows::ApplicationModel::Background::BackgroundTaskDeferral> Deferral;

// ...

Deferral = taskInstance->GetDeferral();