首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SharpDevelop中的System.OutOfMemoryException

SharpDevelop中的System.OutOfMemoryException
EN

Stack Overflow用户
提问于 2014-09-15 02:29:07
回答 1查看 547关注 0票数 0

我不确定为什么我在SharpDevelop中为我的C#程序获得了一个System.OutOfMemoryException。我的程序打开一个Excel工作表,并处理工作表中的一些数据以检查重复项。

下面是完整的异常错误消息:

代码语言:javascript
复制
System.Runtime.InteropServices.COMException: See inner exception(s) for details. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.OutOfMemoryException: Not enough storage is available to complete this operation. (Exception from HRESULT: 0x8007000E (E_OUTOFMEMORY))
   at static Object NetOffice.Invoker.PropertyGet(NetOffice.COMObject comObject, System.String name, System.Object[] paramsArray)
   at Object NetOffice.ExcelApi.Range.get_Value()
   at System.Void excelApp.Program.markDuplicates() in c:\Users\HP\Documents\SharpDevelop Projects\excelApp\excelApp\Program.cs:line 80
   at static System.Void excelApp.Program.Main(System.String[] args) in c:\Users\HP\Documents\SharpDevelop Projects\excelApp\excelApp\Program.cs:line 41

下面是我的完整程序:

它指向markDuplicates()方法中的以下行:第80行:

Object[,] valuesArray = (Object[,])tableRange.Value;

我不知道为什么我会得到这个异常。

我使用的是.NET框架4.5.1。

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input.Manipulations;
using NetOffice.ExcelApi;
using NetOffice.ExcelApi.Enums;
using Excel = NetOffice.ExcelApi.Application;

namespace excelApp
{
    class Program
    {

        Excel excelApplication;
        Workbook workbook;
        Worksheet sheet;

        HashSet<int> mpanHashCodeList;  


        [STAThreadAttribute]
        public static void Main(string[] args)
        {
            Program p = new Program();
            p.openWorkSheet(@"C:\Users\HP\Desktop\Book1.xlsx", 2);    
            p.markDuplicates();      
            Console.ReadKey(true);
        }


        private void openWorkSheet(string path, int worksheet)
        {

            excelApplication = new Excel
            {
                Visible = true,
                ScreenUpdating = true
            };

            try
            {
                workbook = excelApplication.Workbooks.Open(path);
                sheet = (Worksheet)workbook.Worksheets[worksheet];
            }
            catch
            {
                Console.WriteLine("File does not exist");
            }
        }



        private void markDuplicates()
        {

           Range range = sheet.Cells[2,2];
           Range rngLastCell = range.get_End(XlDirection.xlToRight)
                                       .get_End(XlDirection.xlDown);


           // holds the range of cells in the worksheet
           Range tableRange = sheet.Range(range, rngLastCell);

           // holds all the values of the range of cells in the worksheet
           Object[,] valuesArray = (Object[,])tableRange.Value;

           mpanHashCodeList = new HashSet<int>();



           int count = 0;
           for(var i = 1; i <= valuesArray.GetUpperBound(0); i++)
           {      


           // create a new string for each row             
           var rowIdBuilder = new StringBuilder(10);


                   for(var j = 1; j <= valuesArray.GetUpperBound(1); j++)
                   {      
                       switch(j)
                       {
                           case 1:
                               rowIdBuilder.Append(valuesArray[i,j].ToString());   
                               break;
                           case 3:
                               rowIdBuilder.Append(valuesArray[i,j].ToString());  
                               break;
                           case 4:
                               rowIdBuilder.Append(valuesArray[i,j].ToString()); 
                               break;
                           case 6:
                               rowIdBuilder.Append(valuesArray[i,j].ToString());
                               break;        
                       }       
                   }



                  Console.WriteLine(rowIdBuilder.ToString());
                   int hashcode = rowIdBuilder.ToString().GetHashCode();


                   if(mpanHashCodeList.Contains(hashcode))
                   {
                       count++;     
                       mpanHashCodeList.Remove(hashcode);
                   }           
                   else 
                   {    
                       mpanHashCodeList.Add(hashcode);

                   }


           }

           Console.WriteLine(count + " duplicates found");

        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2014-09-15 03:19:56

本机代码通常返回错误代码0x8007000E (E_OUTOFMEMORY),然后将其映射到托管代码中的OutOfMemory异常。例如,当本机堆分配失败或句柄分配失败时,这是常见的返回。

这类问题最好通过Visual Studio中的混合模式调试来诊断。专业人士通常会使用WinDbg/cdb等工具来执行此类任务,并正确解析正确的符号。

简而言之:没有简单的答案。应为脏工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25836612

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档