UWP ExtendedExecution - 请求扩展执行时出错

UWP ExtendedExecution - Error when requesting Extended Execution

我有一个 UWP 应用程序,它在回调中请求扩展执行,该回调在操作开始后触发,如下所示:

private async void HandleOnSyncStart(ISyncStart msg)
{
    // request extended execution
    ExtendedExecutionResult result = await 
      Utils.ExtendedExecutionHelper.RequestExtendedExecutionSessionAsync();
}

,但出现以下错误

The group or resource is not in the correct state to perform the 
 requested operation. (Exception from HRESULT: 0x8007139F)

RequestExtendedExecutionSessionAsync() 方法中的 return 调用中引发错误,如下所示。这是堆栈跟踪:

   at Windows.ApplicationModel.ExtendedExecution.ExtendedExecutionSession..ctor()
   at MyProject.UWP.Utils.ExtendedExecutionHelper.<RequestExtendedExecutionSessionAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at MyProject.UWP.MainPage.<HandleOnSyncStart>d__65.MoveNext()

在 ExtendedExecutionHelper static class 的 RequestExtendedExecutionAsync() 方法中请求扩展执行会话,如下所示:

    public static async Task<ExtendedExecutionResult> RequestExtendedExecutionSessionAsync()
    {
        ClearExtendedExecutionSession();  //first clear if any session granted

        var newSession = new ExtendedExecutionSession();
        newSession.Reason = ExtendedExecutionReason.Unspecified;
        newSession.Description = "Extended Execution Request";
        newSession.Revoked += OnExtendedExecutionSessionRevoked;

        ExtendedExecutionResult result = await newSession.RequestExtensionAsync();
        switch (result)
        {
            case ExtendedExecutionResult.Allowed:
                Debug.WriteLine(string.Format("ExtendedExecutionHelper: ExtendedExecution allowed."));
                session = newSession;
                break;
            case ExtendedExecutionResult.Denied:
                Debug.WriteLine(string.Format("ExtendedExecutionHelper: ExtendedExecution denied."));
                newSession.Dispose();
                break;
        }

        return result; //ERROR
    }

ExtendedExecutionSession class.

的构造函数中抛出此异常

Stacktrace: at Windows.ApplicationModel.ExtendedExecution.ExtendedExecutionSession..ctor()

而这个异常可能是由

引起的

Only one ExtendedExecutionSession can be requested at any time; attempting to create another session while one is currently active will cause an exception to be thrown from the ExtendedExecutionSession constructor.

所以看起来 ClearExtendedExecutionSession 未能处理之前的 运行 会话。如果您的代码是从 this page 中采用的...也许代码有一些细微的错误,如该页面上的一些评论所示。

你可以展示你是如何处理上一个 运行 会话的。