简体   繁体   English

运行循环时出现内存不足异常

[英]Out Of Memory Exception while running a loop

I am confused at this point I have the following code: 我很困惑,我有以下代码:

            while (dal.Read())
            {

                if (FiltersAppliedForThisJob(dal.GetInt(0)))
                   continue;
                var j = 0;
                if (SettingsManager.OpsMgrSettings.JobViewServiceList.IsNotNull() &&
                    SettingsManager.OpsMgrSettings.JobViewServiceList.Count > 0)
                    if (!ThisJobHasASupportedService(dal.GetInt(0)))
                        continue;

                var newJobGridRow = AddRowToJobGridWithJobID(dal.GetInt(0),
                                                             ref j);
                BoldThisJobGridRowIfItIsUpdated(newJobGridRow, dal);
                var jobno = dal2.GetJobNo(dal.GetInt(0).ToString());
                if (locns2view.Length > 0)
                    _jobgrid.Rows[newJobGridRow].Visible = locns2view.Contains(jobno.Substring(0, 2));

                var col = 2;
                if (j < _jobgrid.ColumnCount)
                    for (i = 0; i < str1.Length; i++)
                    {
                        if (str1[i].Length <= 0)
                            continue;

                        if (!dal.IsFieldNull(col))
                            PlaceValuesInJobGridCells(dtype, i, dal2, str1, jobno, newJobGridRow, j, dal, col);
                        j++;
                        col++;
                    }
                if (_jobgrid[0, newJobGridRow].Value.ToString() == prevjobid)
                    _jobgrid.Rows[newJobGridRow].Selected = true;
            }

            SortJobGridColumns();
        }

It seems that the out of memory error is coming from the code above. 似乎内存不足错误来自上面的代码。 However, if I put a MessageBox before I call FiltersAppliedForThisJob(dal.GetInt(0) , I get no error. For example the code below: 但是,如果我在调用FiltersAppliedForThisJob(dal.GetInt(0)之前放一个MessageBox,则不会出错FiltersAppliedForThisJob(dal.GetInt(0) ,例如下面的代码:

            while (dal.Read())
            {
                MessageBox.Show("Done"); // for debugging purposes

                if (FiltersAppliedForThisJob(dal.GetInt(0)))
                   continue;
                var j = 0;
                if (SettingsManager.OpsMgrSettings.JobViewServiceList.IsNotNull() &&
                    SettingsManager.OpsMgrSettings.JobViewServiceList.Count > 0)
                    if (!ThisJobHasASupportedService(dal.GetInt(0)))
                        continue;

                var newJobGridRow = AddRowToJobGridWithJobID(dal.GetInt(0),
                                                             ref j);
                BoldThisJobGridRowIfItIsUpdated(newJobGridRow, dal);
                var jobno = dal2.GetJobNo(dal.GetInt(0).ToString());
                if (locns2view.Length > 0)
                    _jobgrid.Rows[newJobGridRow].Visible = locns2view.Contains(jobno.Substring(0, 2));

                var col = 2;
                if (j < _jobgrid.ColumnCount)
                    for (i = 0; i < str1.Length; i++)
                    {
                        if (str1[i].Length <= 0)
                            continue;

                        if (!dal.IsFieldNull(col))
                            PlaceValuesInJobGridCells(dtype, i, dal2, str1, jobno, newJobGridRow, j, dal, col);
                        j++;
                        col++;
                    }
                if (_jobgrid[0, newJobGridRow].Value.ToString() == prevjobid)
                    _jobgrid.Rows[newJobGridRow].Selected = true;
            }

            SortJobGridColumns();
        }

Also, if I remove the function call FiltersAppliedForThisJob(dal.GetInt(0) , I don't get an the error. 另外,如果删除函数调用FiltersAppliedForThisJob(dal.GetInt(0) ,则不会收到错误消息。

Here is the function code: 这是功能代码:

  private bool FiltersAppliedForThisJob(int p)
    {
        var filter_datefrom = SettingsManager.OpsMgrSettings.MainView_DateFrom;
        var filter_dateto = SettingsManager.OpsMgrSettings.MainView_DateTo;
        var filter_status = SettingsManager.OpsMgrSettings.MainView_JobStatus;
        var jobNumTbl = dal.Get_JobNumber(p);
        var jobStatus = GetJobStatus(p);
        var jobdate = jobNumTbl.Date;
        var jobdate_res = false;
        var jobstat_res = false;
        var isFiltered = false;

        if (jobdate.HasValue)
        {
            //both have values
            if (filter_datefrom.HasValue && filter_dateto.HasValue)
            {
                if (DateTime.Compare(jobdate.Value.Date, filter_datefrom.Value.Date) < 0 ||
                    DateTime.Compare(jobdate.Value.Date, filter_dateto.Value.Date) > 0)
                    jobdate_res = true;
            }
            //only datefrom has value
            else if (filter_datefrom.HasValue)
            {
                if (DateTime.Compare(jobdate.Value.Date, filter_datefrom.Value.Date) < 0)
                    jobdate_res = true;
            }
            //only datato has value
            else if (filter_dateto.HasValue)
            {
                if (DateTime.Compare(jobdate.Value.Date, filter_dateto.Value.Date) > 0)
                    jobdate_res = true;
            }
        }

        if (jobStatus.HasValue)
        {
            if (!filter_status.Equals(2))
                if (!filter_status.Equals(jobStatus))
                    jobstat_res = true;
        }

        if (jobdate_res || jobstat_res)
            isFiltered = true;


        return isFiltered;
    }

Not sure if this is enough information. 不知道这是否足够的信息。 If you need more from me, let me know. 如果您需要我提供更多信息,请告诉我。

Problem solved. 问题解决了。 The application was pulling all of the information from the database before filtering, which is a lot of data. 该应用程序在过滤之前从数据库中提取了所有信息,这是很多数据。 I added extra filtering to the query before loading the data. 我在加载数据之前向查询添加了额外的过滤。 Everything is fine now. 现在一切都很好。 Thanks everyone 感谢大家

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM