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

Wpf 在try-catch c中处理AccessViolation异常#,wpf,mvvm,c#-4.0,try-catch,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(vendori

如何在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以从设备读取血压值。我试图捕获异常,但控件没有超出访问冲突异常出现的“if”语句

好心的建议


感谢

在.NET 4中对AccessViolationException和其他异常的处理已经更改。通常,您不应该捕获这些异常,因此运行时已更改以反映这一点。如果确实需要捕获这些异常,则必须使用
HandledProcessCorruptedStateExceptions
属性对代码进行注释


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

其HandleProcessCorruptedStateExceptions非HandleProcessCorruptedStateExceptions

修复您的代码,不要捕获这些异常。另外,如何在使用外部dll时修复我的代码。?您好,我已放置了属性,但遇到了类似错误的错误(您是否缺少using指令或程序集引用)。Thanks@Tarun:您是否记得导入
System.Runtime.ExceptionServices
命名空间?