繁体   English   中英

断网时使用 StoreContext.CanSilentlyDownloadStorePackageUpdates 会导致应用崩溃

[英]Using StoreContext.CanSilentlyDownloadStorePackageUpdates when the network is disconnected will cause the app to crash

奇怪的是,try catch 无法捕捉到这个异常,所以不知道该怎么办。

代码片段:

[System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptions]
public bool CanSilentlyUpdate()
{
    try
    {
        // Disable the network before executing this method, and an exception is thrown:
        //     System.AccessViolationException :“Attempted to read or write protected memory. 
        //     This is often an indication that other memory is corrupt.”
        // Then the app crashes
        return StoreContext.GetDefault().CanSilentlyDownloadStorePackageUpdates;
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex);
        return false;
    }
}

事件查看器中的一些错误日志:

应用程序:MyApp.exe 框架版本:v4.0.30319 描述:进程因未处理的异常而终止。 异常信息:Windows.Services.Store.StoreContext.get_CanSilentlyDownloadStorePackageUpdates() 处的 System.AccessViolationException

我知道我可以通过判断网络状态来判断是否调用StoreContext.CanSilentlyDownloadStorePackageUpdatesby ,但是当我使用StoreContext.TrySilentDownloadStorePackageUpdatesAsync升级app时,如果升级过程中网络被禁用,app还是会crash。

我认为这是同一类问题,所以我希望能有更好的方法来解决它。

与工程师讨论后得出结论,目前在UI线程中无法捕获StoreContext.TrySilentDownloadStorePackageUpdatesAsync在后台抛出的异常。

当前推荐的方法是在执行此方法之前检查网络可用性( NetworkInformation.NetworkStatusChanged )。 当网络状态发生变化时,需要用户重新进行下载。 或者使用位于 UI 线程中的RequestDownloadStorePackageUpdatesAsync方法来执行下载请求。

谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM