【发布时间】: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