C# System.AggregateException:发生一个或多个错误---&燃气轮机;System.ArgumentOutOfRangeException

C# System.AggregateException:发生一个或多个错误---&燃气轮机;System.ArgumentOutOfRangeException,c#,parallel.foreach,C#,Parallel.foreach,我有一张支票 它只将数据放入datatable 但它也会有例外 如何修复它 谢谢 Parallel.ForEach(this.dt.AsEnumerable(), opt, r => string inTime = string.Empty; string outTime = string.Empty; string shiftID = string.Empty; string shi

我有一张支票

它只将数据放入datatable 但它也会有例外

如何修复它

谢谢

   Parallel.ForEach(this.dt.AsEnumerable(), opt, r =>
  string inTime = string.Empty;
                string outTime = string.Empty;
                string shiftID = string.Empty;
                string shiftintime = string.Empty;
                string shiftouttime = string.Empty;
                decimal leaveHours = 0;
                string tempNight = string.Empty;
                CommandAPI_Prg.GetShiftID(r["EmployeeID"].ToString(), r["CDate"].ToDateTime(), out shiftID); // 取班別
                CommandAPI_Prg.LogBoxTime(Env.User.Factory, r["EmployeeID"].ToString(), r["CDate"].ToDateTime(), out inTime, out outTime); // 取刷卡時間
                CommandAPI_Prg.DayOfShift2(shiftID, out shiftintime, out shiftouttime); // 取班別時間
                DataTable dayOfLeave = CommandAPI_Prg.DayOfLeave(Env.User.Factory, r["EmployeeID"].ToString(), r["CDate"].ToDateTime()); // 取請假資訊
                DataTable dayOfOverTime = CommandAPI_Prg.DayOfOverTime(Env.User.Factory, r["EmployeeID"].ToString(), r["CDate"].ToDateTime()); // 取加班資訊

                r["ShiftID"] = shiftID; // 實際班表
                r["shiftintime"] = shiftintime;
                r["shiftouttime"] = shiftouttime;
                r["InTime"] = inTime;
                r["OutTime"] = outTime;
}
             );
我一直得到以下例外情况:

System.AggregateException:发生一个或多个错误。-->System.ArgumentOutOfRange异常: 参数名称:索引

System.AggregateException: One or more errors occurred. ---> System.ArgumentOutOfRangeException: 索引超出範圍。必須為非負數且小於集合的大小。
Parameter name: index
   at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at System.Data.RecordManager.NewRecordBase()
   at System.Data.DataTable.NewRecord(Int32 sourceRecord)
   at System.Data.DataRow.BeginEditInternal()
   at System.Data.DataRow.set_Item(DataColumn column, Object value)
   at System.Data.DataRow.set_Item(String columnName, Object value)
   at Sci.PamsPH.Payroll.PayrollP01.<ButOK_Click>b__6_1(DataRow r) in D:\SystemGit2017\PamsPH_Formal\Sci.PamsPH.Payroll\PayrollP01.cs:line 350
   at System.Threading.Tasks.Parallel.<>c__DisplayClass42_0`2.<PartitionerForEachWorker>b__1()
   at System.Threading.Tasks.Task.InnerInvoke()
   at System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask)
   at System.Threading.Tasks.Task.<>c__DisplayClass176_0.<ExecuteSelfReplicating>b__0(Object )
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at System.Threading.Tasks.Parallel.PartitionerForEachWorker[TSource,TLocal](Partitioner`1 source, ParallelOptions parallelOptions, Action`1 simpleBody, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally)
   at System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable`1 source, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally)
   at System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable`1 source, ParallelOptions parallelOptions, Action`1 body)
   at Sci.PamsPH.Payroll.PayrollP01.ButOK_Click(Object sender, EventArgs e) in D:\SystemGit2017\PamsPH_Formal\Sci.PamsPH.Payroll\PayrollP01.cs:line 201
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at Ict.Win.UI.Button.OnClick(EventArgs e)
   at Sci.Win.UI.Button.OnClick(EventArgs e) in D:\System2017\PublicClass\Sci\Win\UI\Button.cs:line 253
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
---> (Inner Exception #0) System.ArgumentOutOfRangeException: 索引超出範圍。必須為非負數且小於集合的大小。
Parameter name: index
   at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at System.Data.RecordManager.NewRecordBase()
   at System.Data.DataTable.NewRecord(Int32 sourceRecord)
   at System.Data.DataRow.BeginEditInternal()
   at System.Data.DataRow.set_Item(DataColumn column, Object value)
   at System.Data.DataRow.set_Item(String columnName, Object value)
   at Sci.PamsPH.Payroll.PayrollP01.<ButOK_Click>b__6_1(DataRow r) in D:\SystemGit2017\PamsPH_Formal\Sci.PamsPH.Payroll\PayrollP01.cs:line 350
   at System.Threading.Tasks.Parallel.<>c__DisplayClass42_0`2.<PartitionerForEachWorker>b__1()
   at System.Threading.Tasks.Task.InnerInvoke()
   at System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask)
   at System.Threading.Tasks.Task.<>c__DisplayClass176_0.<ExecuteSelfReplicating>b__0(Object )<---
System.AggregateException:发生一个或多个错误。-->System.ArgumentOutOfRange异常:索引超出範圍。必須為非負數且小於集合的大小。
参数名称:索引
在System.ThrowHelper.ThrowarGumentofrangeException处(ExceptionArgument参数,ExceptionResource资源)
位于System.Collections.Generic.List`1.get_项(Int32索引)
位于System.Data.RecordManager.NewRecordBase()处
位于System.Data.DataTable.NewRecord(Int32 sourceRecord)
位于System.Data.DataRow.BeginEditInternal()处
在System.Data.DataRow.set_项(DataColumn列,对象值)
在System.Data.DataRow.set_项(字符串列名,对象值)
在D:\SystemGit2017\PamsPH\u Formal\Sci.PamsPH.Payroll\PayrollP01.b_uu6_u1(数据行r)中的Sci.PamsPH.Payroll.PayrollP01(数据行r):第350行
在System.Threading.Tasks.Parallel.c__uDisplayClass42_0`2.b_u 1()中
在System.Threading.Tasks.Task.InnerInvoke()中
位于System.Threading.Tasks.Task.InnerInvokeWitchArg(任务子任务)
在System.Threading.Tasks.Task.c\u显示器上显示Class176\u 0.b\u 0(对象)
---内部异常堆栈跟踪的结束---
位于System.Threading.Tasks.Task.ThrowifeException(布尔值IncludeTaskCanceledException)
在System.Threading.Tasks.Task.Wait(Int32毫秒计时,CancellationToken CancellationToken)
在System.Threading.Tasks.Task.Wait()中
在System.Threading.Tasks.Parallel.PartitionerForEachWorker[TSource,TLocal](分区器'1 source,ParallelOptions ParallelOptions,Action'1 simpleBody,Action'2 bodyWithState,Action'3 bodyWithStateAndIndex,Func'4 bodyWithStateAndLocal,Func'5 bodyWithEverything,Func'1 localInit,Action'1 localFinally)
在System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable`1 source,ParallelOptions ParallelOptions,Action`1 body,Action`2 bodyWithState,Action`3 bodyWithStateAndIndex,Func`4 bodyWithStateAndLocal,Func`5 bodyWithEverything,Func`1 localInit,Action`1 localFinally)
在System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable`1源,ParallelOptions,Action`1体)
在Sci.PamsPH.Payroll.PayrollP01.ButOK\单击D:\SystemGit2017\PamsPH\u Formal\Sci.PamsPH.Payroll\PayrollP01.cs中的(对象发送者,事件参数e):第201行
在System.Windows.Forms.Control.OnClick(EventArgs e)中
在System.Windows.Forms.Button.OnClick(EventArgs e)中
在Ict.Win.UI.Button.OnClick(EventArgs e)
在D:\System2017\PublicClass\Sci\Win\UI\Button.cs:第253行中的Sci.Win.UI.Button.OnClick(EventArgs e)处
在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs-mevent)上
在System.Windows.Forms.Control.WmMouseUp(Message&m、MouseButtons按钮、Int32单击)
位于System.Windows.Forms.Control.WndProc(Message&m)
位于System.Windows.Forms.ButtonBase.WndProc(Message&m)
在System.Windows.Forms.Button.WndProc(Message&m)中
在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)中
在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)中
在System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd、Int32 msg、IntPtr wparam、IntPtr lparam)中
--->(内部异常#0)System.ArgumentOutOfRangeException:索引超出範圍。必須為非負數且小於集合的大小。
参数名称:索引
在System.ThrowHelper.ThrowarGumentofrangeException处(ExceptionArgument参数,ExceptionResource资源)
位于System.Collections.Generic.List`1.get_项(Int32索引)
位于System.Data.RecordManager.NewRecordBase()处
位于System.Data.DataTable.NewRecord(Int32 sourceRecord)
位于System.Data.DataRow.BeginEditInternal()处
在System.Data.DataRow.set_项(DataColumn列,对象值)
在System.Data.DataRow.set_项(字符串列名,对象值)
在D:\SystemGit2017\PamsPH\u Formal\Sci.PamsPH.Payroll\PayrollP01.b_uu6_u1(数据行r)中的Sci.PamsPH.Payroll.PayrollP01(数据行r):第350行
在System.Threading.Tasks.Parallel.c__uDisplayClass42_0`2.b_u 1()中
在System.Threading.Tasks.Task.InnerInvoke()中
位于System.Threading.Tasks.Task.InnerInvokeWitchArg(任务子任务)

在System.Threading.Tasks.Task.c\u DisplayClass176\u 0.b\u 0(对象)中,您尝试过调试它吗?您的实际异常是System.ArgumentOutOfRangeException,因此在某些情况下,您似乎正在使用不存在的索引查找列表/数组/集合。如果我使用foreach not Parallel.foreach,则不会发生此问题。请向我们展示整个方法。这里有很多bug。例如,您的每个
out
变量(例如
shiftID
)必须在
ForEach
中声明,而不是在其外部声明。您是否尝试过调试它?您的实际异常是System.ArgumentOutOfRangeException,因此在某些情况下,您似乎正在使用不存在的索引查找列表/数组/集合。如果我使用foreach not Parallel.foreach,则不会发生此问题。请向我们展示整个方法。这里有很多bug。例如,每个
out
变量(例如
shiftID
)必须在
ForEach
内部声明,而不是在其外部声明。可能的