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

喜欢而已 提交于 2019-12-25 06:04:09

问题


Regarding my universal app project, I am getting compilation warning:

warning C4451: 'BackgroundServerTasks::ServerTask::Run::::deferral': Usage of ref class 'Windows::ApplicationModel::Background::BackgroundTaskDeferral' inside this context can lead to invalid marshaling of object across contexts

note: Consider using 'Platform::Agile' instead

in 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 which dismisses the problem but I am not sure if it is applicable in this case.


回答1:


It surprises me that Windows::ApplicationModel::Background::BackgroundTaskDeferral is not agile.

Try completing the deferral in the same context, i.e.:

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

Another option is to use a Platform::Agile<> wrapper:

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

// ...

Deferral = taskInstance->GetDeferral();


来源:https://stackoverflow.com/questions/32061672/warning-c4451-usage-of-ref-class-backgroundtaskdeferral-can-lead-to-invalid-mar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!