wpf - 在 try catch c# 中处理 AccessViolation 异常

标签 wpf mvvm c#-4.0 try-catch

如何在 try-catch 块中捕获 AccessViolation 异常:

这是下面的代码:

public static BP GetBloodPressure(string vendorid, string productid)
{
    BP Result = new BP();
    try
    {
        GETBPData BPreadings = new GETBPData();
        UInt16 VendorId = Convert.ToUInt16(vendorid, 16);
        UInt16 ProductId = Convert.ToUInt16(productid, 16);

        if (HealthMonitorData.HidDataTap_GetBloodPressure(VendorId, ProductId, ref BPreadings)) // error here
        {

            if (BPreadings.ucSystolic == 0 && BPreadings.ucDiastolic == 0 && BPreadings.DeviceId1 == 0 && BPreadings.DeviceId2 == 0 && BPreadings.ucPulse == 0)
            {
                Result = null;

            }
            else
            {
                Result.UcSystolic = BPreadings.ucSystolic;
                Result.UcDiastolic = BPreadings.ucDiastolic;
                Result.UcPulse = BPreadings.ucPulse;
                Result.DeviceId1 = BPreadings.DeviceId1;
                Result.DeviceId2 = BPreadings.DeviceId2;
            }
        }
    }
    catch (Exception ex)
    {

    }
        return Result;
}

我正在导入一个 dll 以从设备读取血压值。我已 try catch 异常,但控制并未超出访问冲突异常即将到来的“if”语句。

请推荐?

谢谢

最佳答案

处理 AccessViolationExceptions 和其他 corrupted state exceptions在 .NET 4 中已更改。通常您不应捕获这些异常,因此运行时已更改以反射(reflect)这一点。如果你真的需要捕捉这些,你必须用 HandledProcessCorruptedStateExceptions 注释代码。属性。

请记住,行为的改变是有充分理由的。大多数应用程序将无法以任何有意义的方式处理这些异常,因此不应捕获它们。

关于wpf - 在 try catch c# 中处理 AccessViolation 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4342677/

相关文章:

c# - 在功能区上重复使用图像会导致 XP 崩溃

c# - 将用户控件的子属性直接公开为用户控件的属性

c# - 具有自定义用户控件的多阶段数据绑定(bind)

android - 在android中使用MVVM设计模式时错误找不到符号类ViewModel

c#-4.0 - SetUp 或 TearDown 方法的签名无效 - 我做错了什么?

xml - 为什么 XmlDocument 在 .NET 4 中不是动态的?

c# - WPF Canvas 中的网格线

html - 如何为 silverlight 页面启用自动滚动

wpf - 如何使用 WPF Toolkit 将多个图表与多个系列绑定(bind)?

asp.net-mvc-4 - MVC 4 表单例份验证(无限登录)