WinRT 中后台任务类的声明

时间:2021-08-29 01:02:02

要实现后台任务,需要实现IBackgroundTask接口

     public sealed class SimpleTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
// TODO
}
}

假设需要用到异步方法,则要这么写:

     public sealed class SimpleAsyncTask : IBackgroundTask
{
public async void Run(IBackgroundTaskInstance taskInstance)
{
BackgroundTaskDeferral deferral = taskInstance.GetDeferral();
// TODO
deferral.Complete();
}
}