Ya Wang
Ya Wang

Reputation: 1808

Multithreading List AddRange

Issue is that TotalRows is about 71800 where the workList only returns 718 which is only the first result of the Task. I have the WaitAll there but it seems to finish as soon as the first task is done.

TotalRows = GetRowCount();
        var lastRecord = 0;
        List<tmpWBITEMALL> workList = new List<tmpWBITEMALL>();
        for (int i = 0; i < 100; i++)
        {
            var tmpI = i;
            gatherTmpTasks.Add(Task.Factory.StartNew(() =>
            {
                var context = new AS400_PIM5ContextDataContext();
                context.CommandTimeout = 0;
                int amount = (TotalRows / 100);
                int tmplastRecord = lastRecord;
                Interlocked.Add(ref lastRecord, amount); 
                Console.WriteLine("Getting rows " + tmplastRecord+ " to " +  (tmplastRecord + amount));
                var pagedResult = context.ExecuteQuery<tmpWBITEMALL>("SELECT * FROM (SELECT ROW_NUMBER() OVER ( ORDER BY Id ) AS RowNum, * from tmpWBITEMALL) AS RowConstrainedResult WHERE   RowNum >= " + tmplastRecord+ " AND RowNum < " + amount + " ORDER BY RowNum");
                lock (listLock)
                    workList.AddRange(pagedResult);
                context.Dispose();
            })); 
        }
        Task.WaitAll(gatherTmpTasks.ToArray());
        Console.WriteLine("total work: " + workList.Count + " tasks: " + gatherTmpTasks.Count); 

So as reference gatherTmpTasks.Count returns 100 but workList.Count is only 718 where as listLock is just a static new object(). If didn't notice already I am using LINQ to SQL

Anyone have ideas why my list isn't the same size as TotalRows?

Upvotes: 0

Views: 454

Answers (1)

Ya Wang
Ya Wang

Reputation: 1808

" AND RowNum < " + amount: amount is always 718, so you are asking the query to always return things between tmplastRecord and 718, NOT inbetween tmplastRecord and tmplastRecord + amount. I think you just need to change to " AND RowNum < " + (tmplastRecord + amount)

Wise man

Upvotes: 1

Related Questions