请教 源数组长度不足。请检查 srcIndex 和长度以及数组的下限。

吾非大神 2012-10-30 06:40:06
在执行下面的代码的时候,不定时的报错
"源数组长度不足。请检查 srcIndex 和长度以及数组的下限。"

backWrok.DoWork += new DoWorkEventHandler((sender, args) =>
{
List<Hashtable> listResultAll = new List<Hashtable>();
for (int p = 1; p <= pageCount; p++)
{
try
{
listResultAll.AddRange(BatchSend(claimList.Skip((p - 1) * groupNum).Take(groupNum).ToList(), ref prcoCount));
}
catch
{
throw;
}
}
args.Result = listResultAll;
});

堆栈跟踪
在 System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
在 System.Collections.Generic.List`1.CopyTo(T[] array, Int32 arrayIndex)
在 System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
在 System.Collections.Generic.List`1.AddRange(IEnumerable`1 collection)
在 MainForm.<>c__DisplayClass5d.<AsyncDataSend>b__5a(Object sender, DoWorkEventArgs args) 位置 E:\MainForm.cs:行号 2070
在 System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
在 System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
...全文
1097 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
吾非大神 2012-11-13
  • 打赏
  • 举报
回复
我用了网友的方法 初始化List<T> 集合时,指定了集合的固定长度。 之后问题没有再重现。
吾非大神 2012-11-03
  • 打赏
  • 举报
回复

            int subCount = sendData.Count;
            List<Hashtable> listRelust = new List<Hashtable>();
            foreach (ClaimDocument data in sendData)
            {               
                //函数定义
                //AsyncClaimSend(ClaimDocument data, AsyncClaimSendCompleted callBack)
                AsyncClaimSend(data, (result) =>  
                                    {
                                        try
                                        {
                                            listRelust.Add(result);//List<T>.Add()的问题????
                                        }
                                        catch (Exception ex)
                                        { 
                                            //在这个exception中 提示错误信息
                                            MessageBox.Show(ex.Message);
                                        }
                                        lock (this)
                                        {
                                            subCount--;
                                        }
                                    });
            }
            while (subCount > 0)
            {
                continue;
            }
            prcoCount += listRelust.Count;

            SetMessageText(string.Format("已处理了{0}条数据\r\n", prcoCount), (int)(((decimal)prcoCount / (decimal)totalCount) * 100));
            return listRelust;
大致代码如上,我找了下 有人说是 List<T>.Add();当内部数组长度不够的时候,会新建数组,拷贝原有数组 ,但是我用小程序 测试这个方法,增加10W个People对象,也没出现异常呢??
宝_爸 2012-11-01
  • 打赏
  • 举报
回复
AddRange里出错了,建议楼主把
listResultAll.AddRange(BatchSend(claimList.Skip((p - 1) * groupNum).Take(groupNum).ToList(), ref prcoCount));

拆成多个语句,分别分析测试。
吾非大神 2012-11-01
  • 打赏
  • 举报
回复
没人呢?
XBodhi. 2012-10-31
  • 打赏
  • 举报
回复
listResultAll.AddRange(BatchSend(claimList.Skip((p - 1) * groupNum).Take(groupNum).ToList(), ref prcoCount));

这里有问题
吾非大神 2012-10-31
  • 打赏
  • 举报
回复
好像不是这个原因
吾非大神 2012-10-31
  • 打赏
  • 举报
回复
楼上说的对。。
但怎么解决呢。
wuyq11 2012-10-30
  • 打赏
  • 举报
回复
判断claimList长度与(p - 1) * groupNum大小

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧