扩展执行工作不正常? [英] Extended execution not working properly?

查看:159
本文介绍了扩展执行工作不正常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能够获得 ExtendedExecution 正常工作。问题是,在撤销未烧制事件,直至执行完毕。如果我们把一个样本:



<预类=郎-CS prettyprint-覆盖> 专用异步无效OnSuspending(对象发件人,SuspendingEventArgs E)
{
的Debug.WriteLine(在应用挂起);
VAR延期= e.SuspendingOperation.GetDeferral();
使用(VAR会话=新ExtendedExecutionSession())
{
session.Reason = ExtendedExecutionReason.SavingData;
session.Description =上传数据;
session.Revoked + =(S,A)=> {的Debug.WriteLine($扩展执行被撤销,因为{} a.Reason); };
VAR的结果=等待session.RequestExtensionAsync();
如果(结果== ExtendedExecutionResult.Denied)的Debug.WriteLine(扩展执行失败);
,否则
{
的Debug.WriteLine(执​​行);
等待Task.Run(()=> {Task.Delay(9000).Wait();的Debug.WriteLine(完成任务);});
的Debug.WriteLine(执​​行完毕);
}
的Debug.WriteLine(执​​行后暂停);
}
deferral.Complete();
}

的文档指出撤销事件应当在恢复应用程序被解雇,但如果你尝试用调试器附着的代码,那么你会看到调试输出似乎看起来不错,但你必须等待9000毫秒它展现出来。这意味着代码被挂起,直到会话结束。



最大的问题是,如果你开除这个没有调试器连接,启动应用程序,暂停,然后继续,你会看到一个黑色的屏幕几秒钟,然后OS会终止您的应用程序。



我缺少的东西吗?有没有人得到它的正常工作?


解决方案

的使用量等待工作导致您继续任务仍然主线程使你不得不等待与黑色的屏幕上。
记住等待的行为是计划执行到调度,而不是开始一个新的线程,NOR安排其执行到线程池。作为结果,没有更多的UI消息可以被处理,直到延迟()结束。



只是执行您的时间在一个新的线程耗时的操作,但要确保保持会话打开状态,直至结束。



看看这个的 https://msdn.microsoft.com/en-us/magazine/jj991977.aspx 以获取有关如何执行一个很好的洞察力是sheduled


I'm not able to get ExtendedExecution to work properly. The problem is that the Revoked event is not being fired until the execution is finished. If we take a sample:

private async void OnSuspending(object sender, SuspendingEventArgs e)
{
    Debug.WriteLine("Suspending in app");
    var deferral = e.SuspendingOperation.GetDeferral();
    using (var session = new ExtendedExecutionSession())
    {
        session.Reason = ExtendedExecutionReason.SavingData;
        session.Description = "Upload Data";
        session.Revoked += (s, a) => { Debug.WriteLine($"Extended execution revoked because of {a.Reason}"); };
        var result = await session.RequestExtensionAsync();
        if (result == ExtendedExecutionResult.Denied) Debug.WriteLine("Extended execution failed");
        else
        {
            Debug.WriteLine("Executing");
            await Task.Run(() => { Task.Delay(9000).Wait(); Debug.WriteLine("Finished the task"); });
            Debug.WriteLine("Executing finished");
        }
        Debug.WriteLine("Suspending after execution");
    }
    deferral.Complete();
}

The documentation states that Revoked event should be fired upon resuming the app, but if you try the code with debugger attached, then you will see that debug output seems to look ok, but you have to wait 9000 ms for it to show up. This means that the code is suspended until the session finishes.

The biggest problem is that if you fire this without debugger attached, launch the app, suspend and then resume, you will see a black screen for few seconds and then OS will terminate your app.

Am I missing something? Has anybody got it working correctly?

解决方案

The usage of the await and Task is causing that your continued Task remains on the main thread making you have to wait with the black screen. Remember that the await behavior is to schedule the execution into the Dispatcher, NOT to begin a new thread, NOR schedule its execution into the ThreadPool. As consequence, No more UI messages can be processed until the Delay() ends.

Just perform your time-consuming operation on a new thread, but ensure to keep the session open until it ends.

Take a look of this https://msdn.microsoft.com/en-us/magazine/jj991977.aspx to get a good insight about how execution is sheduled

这篇关于扩展执行工作不正常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆