问题
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