【问题标题】:Using StoreContext.CanSilentlyDownloadStorePackageUpdates when the network is disconnected will cause the app to crash断网时使用 StoreContext.CanSilentlyDownloadStorePackageUpdates 会导致应用崩溃
【发布时间】:2020-10-29 16:56:59
【问题描述】:

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

代码sn-p:

[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 描述: 由于未处理的异常,进程被终止。异常信息: System.AccessViolationException 在 Windows.Services.Store.StoreContext.get_CanSilentlyDownloadStorePackageUpdates()

我知道我可以判断是否调用StoreContext.CanSilentlyDownloadStorePackageUpdatesby判断网络状态,但是当我使用StoreContext.TrySilentDownloadStorePackageUpdatesAsync升级应用时,如果在升级过程中禁用网络,应用仍然会崩溃。

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

【问题讨论】:

  • 您好,TrySilentDownloadStorePackageUpdatesAsync 方法将启动静默下载。当方法完成时,意味着下载已经开始。如果在下载过程中由于网络问题而发生异常,则放置在该方法上的 Try-Catch 可能无法捕捉到异常。您可以尝试处理App.UnhandledException 事件,应用内未捕获的异常最终会在此处冒泡。
  • @RichardZhang-MSFT 你好,Richard,谢谢你的回复,但是为什么我不能使用 Try-catch 来捕捉 CanSilentlyDownloadStorePackageUpdates 引起的异常呢?我认为这两种方法引发了相同的异常(可能是非托管异常?),所以都不能被捕获。
  • 忘了说,我的应用是桌面桥接应用,我可以使用App.Dispatcher.UnhandledException来捕捉我抛出的错误,但是无法捕捉到System.AccessViolationException
  • 我写了一个很简单的demo上传到onedriver,你可以直接复现问​​题,如果你能花点时间帮我看看就好了

标签: c# uwp desktop-bridge


【解决方案1】:

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

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

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多