zoukankan      html  css  js  c++  java
  • 未处理AccessViolationException 异常

     在进行arcgis的GP操作时,当操作栅格图像的拼接时,报错:

    AccessViolationException: 尝试读取或写入受保护的内存

     原以为可以通过try catch屏蔽掉错误,不至于程序崩溃,但是,catch不起作用。不知道为什么?后来google才发现。问题:大致意思是Net能处理托管的Structured Error Handling (SEH)异常,

    但是对于非托管Corrupted State Exceptions (CSE)异常却不能捕获,如果想捕获此种类型异常,需要在配置文件中,添加一下内容

      <runtime>
        <legacyCorruptedStateExceptionsPolicy enabled="true" />
      </runtime>

    在try catch中添加了AccessViolationException的捕获,代码如下:

      private static bool BeginRunGP(IGPProcess process, ITrackCancel TC, Action<string> pMessage)
            {
                bool result = false;
                try
                {
    
                    if (m_geoprocessor == null)
                    {
                        m_geoprocessor = new Geoprocessor();
                    }
    
                    m_geoprocessor.OverwriteOutput = true;
                    //m_geoprocessor.MessagesCreated += m_geoprocessor_MessagesCreated;
                    //showMessage("-------------------------------------------------------------------------------------------------------" + Environment.NewLine);
                    //showMessage(string.Format("开始时间:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")) + Environment.NewLine);
                    if (pMessage != null)
                    {
                        pMessage("GP:" + process.ToolName);
                    }
                    IGeoProcessorResult2 geoProcessorResult = m_geoprocessor.Execute(process, null) as IGeoProcessorResult2;
                    if (geoProcessorResult != null && geoProcessorResult.Status == esriJobStatus.esriJobSucceeded)
                    {
                        result = true;
                    }
    
                    if (geoProcessorResult != null)
                    {
                        for (int i = 0; i < geoProcessorResult.MessageCount; i++)
                        {
                            string str = geoProcessorResult.GetMessages(i);
                            if (pMessage != null)
                            {
                                pMessage(str);
                            }
                        }
                    }
                    if (pMessage != null)
                    {
                        pMessage("GP:" + process.ToolName + " 结束");
                    }
                    result = true;
                }
                catch (System.AccessViolationException ex)
                { //   AccessViolationException异常捕获
                    result = false;
                }
                catch (Exception ex)     //   COMException
                {
                    result = false;
                    if (pMessage != null)
                    {
                        pMessage("GP:" + process.ToolName + " 错误");
                        pMessage(ex.Message);
                        LogClass.PubLog.ErrorFormat("GP:{0} 错误,错误原因是:{1}", process.ToolName, ex.Message);
                    }
                    LogClass.PubLog.ErrorFormat("GP:{0} 错误,错误原因是:{1}", process.ToolName, ex.Message);
                }
                finally
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    for (int i = 0; i < m_geoprocessor.MessageCount; i++)
                        sb.AppendLine(m_geoprocessor.GetMessage(i));
    
                }
                return result;
            }

    下面是他的英文解释

     
    In .NET 4.0, the runtime handles certain exceptions raised as Windows Structured Error Handling (SEH) errors as indicators of Corrupted State. 
    These Corrupted State Exceptions (CSE) are not allowed to be caught by your standard managed code. I won't get into the why's or how's here. Read this article about CSE's in the .NET 4.0 Framework:

    http://msdn.microsoft.com/en-us/magazine/dd419661.aspx#id0070035

    But there is hope. There are a few ways to get around this:

    (1)Recompile as a .NET 3.5 assembly and run it in .NET 4.0.

    (2)Add a line to your application's config file under the configuration/runtime element:<legacyCorruptedStateExceptionsPolicy enabled="true"/>

    (3)Decorate the methods you want to catch these exceptions in with the HandleProcessCorruptedStateExceptions attribute. See http://msdn.microsoft.com/en-us/magazine/dd419661.aspx#id0070035 for details.

    For more reference:http://connect.microsoft.com/VisualStudio/feedback/details/557105/unable-to-catch-accessviolationexception

  • 相关阅读:
    用JS + WCF打造轻量级WebPart
    提高WCF服务并发能力的简单处理办法
    利用JQuery实现更简单的Ajax跨域请求
    WCF Testing Tool(转)
    [转贴]一个有趣的布局
    [转贴].net中上传视频并将各种视频文件转换成.flv格式
    IE5,IE6,IE7,IE8的css兼容性列表[转自MSDN]
    [转贴]Castle 开发系列文章
    ie6,ie7,ff 的css兼容hack写法
    ExtJs学习笔记(23)ScriptTagProxy+XTemplate+WCF跨域取数据
  • 原文地址:https://www.cnblogs.com/cglNet/p/6913024.html
Copyright © 2011-2022 走看看