我需要实现实时媒体流的定时录制功能。条件之一是应用程序不必运行。 (它应该在后台运行) 我有一个方法 StartSchedulerAsync,启动时会无限检查是否有必要开始录制。所以,我需要在后台任务中运行这个方法(当PC启动时,以及当程序第一次启动时),以便它在后台运行无限长的时间。 我读过有关 ExtendedExecutionSession 的内容,但据我了解,它在计算机启动时和在后台时不起作用。
我试过了,效果很好,但只有在程序打开的情况下。
private async Task RequestExtendedExecution()
{
var newSession = new ExtendedExecutionSession
{
Reason = ExtendedExecutionReason.Unspecified,
Description = "Long Running Radio Recorder"
};
var result = await newSession.RequestExtensionAsync();
switch (result)
{
case ExtendedExecutionResult.Allowed:
Task.Run(async () => await BackgroundRecordingService.StartSchedulerAsync());
break;
default:
case ExtendedExecutionResult.Denied:
break;
}
}
正如 Raymond 所说,UWP 后台任务无法无限期运行。官方文档中后台任务资源限制
后台任务限制为 30 秒的挂钟使用时间。