0

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.

An Hoa
  • 1,207
  • 12
  • 38

1 Answers1

0

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();
kiewic
  • 15,852
  • 13
  • 78
  • 101