间歇性异常:System.InvalidOperationException:集合已被修改;枚举操作可能无法执行

时间:2016-05-13 09:24:25

标签: c# winforms invalidoperationexception

我在多个服务器上运行的WinForms应用程序中收到间歇性异常。这些失败似乎没有一致的前兆,因为相同的代码将在同一天成功执行,然后在第二天失败。

以下是发生异常的代码:

  Typeface iconCalendar = FontManager.getTypeface(getContext(), FontManager.FONTAWESOME);
  jcustomerDobIcon.setTypeface(iconCalendar);

以下是异常的堆栈跟踪:

private void Cr22(RCItem anItem)
    {
        string[] elements = anItem.param.Split('#');

        if (elements.Length == 3)
        {
            if (OnMessage != null)
            {
                OnMessage("\tPartner : " + anItem.partner);
                OnMessage("\tProgram : " + anItem.program);
            }

            AddLogMessage(anItem, "Start of processing");
            AddLogMessage(anItem, "Calculating combined total");

            RCDateRange range = GetMonthCalcDays(anItem, anItem.date, 1);
            DataTable table = new DataTable();

            table.Columns.Add("Date", typeof(string));
            table.Columns.Add("Amount", typeof(double));

            foreach (string dateString in range.allDates)
            {
                string sql = string.Format("SELECT c.Short_Date AS [Date], c.Day FROM dbo.CS_Calendar c " +
                    "INNER JOIN dbo.CS_Calendar PreviousDay ON CONVERT(INT, c.Previous_Date) = CONVERT(INT, PreviousDay.Short_Date) WHERE c.Short_Date = '{0}'", dateString);

                int dayOfWeek = 0;

                using (OleDbCommand cmd = new OleDbCommand(sql, _aConn))
                {
                    cmd.CommandTimeout = _timeout;

                    using (OleDbDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader != null && reader.HasRows)
                            while (reader.Read())
                                dayOfWeek = Convert.ToInt32(reader.GetValue(1));
                    }
                }

                if (dayOfWeek == 0)
                {
                    AddLogMessage(anItem, "Invalid Date Supplied");
                    return;
                }

                List<string> prevDaysList = new List<string>();

                if (dayOfWeek > 1 && dayOfWeek < 7)
                {
                    if (dayOfWeek >= 2 && dayOfWeek <= 4)
                        prevDaysList = GetPreviousXDays(dateString, 5, false);
                    else
                        prevDaysList = GetPreviousXDays(dateString, 3, false);

                    sql = string.Format("**redacted**", StringListAsIn(prevDaysList));

                    AddLogMessage(anItem, sql);

                    double val = 0;

                    // Get the values for the specified date range in the previous month using the specified SQL query
                    using (OleDbCommand cmd = new OleDbCommand(sql, _aConn))
                    {
                        cmd.CommandTimeout = _timeout;

                        using (OleDbDataReader reader = cmd.ExecuteReader())
                        {
                            if (reader != null && reader.HasRows)
                                while (reader.Read())
                                    val = val + Convert.ToDouble(reader.GetValue(1));
                        }
                    }

                    AddLogMessage(anItem, string.Format("Day value for {0} = {1}", dateString, val));

                    // Add fixed sum of 18,000 to the day total
                    val += 18000.00;
                    AddLogMessage(anItem, string.Format("Adding Fixed Sum of 18,000, day value is now: {0}", val));

                    // Add the day total to the results table
                    DataRow row = table.NewRow();
                    row["Date"] = dateString;
                    row["Amount"] = val;
                    table.Rows.Add(row);
                }
            }

            // Find the highest day value in the results table
            double max = Convert.ToDouble(table.Compute("max(Amount)", string.Empty));
            anItem.result = Math.Round(max, 2); 
            AddLogMessage(anItem, string.Format("Max value is: {0}", anItem.result));
        }
        else
            AddLogMessage(anItem, string.Format("Bad Parameters for {0}", anItem));

        AddLogMessage(anItem, "End of process");
    }

0 个答案:

没有答案