I am List of Object for send to api .i used parallel thread below.
code
List<object> data ;//contain data
result =new Dictionary<decimal, object>();
var threadCount = 4;
if (data.Count < threadCount)
{
threadCount = data.Count;
}
var pageSize = threadCount > 0 ? Convert.ToInt32(Math.Ceiling((Convert.ToDecimal(data.Count) / threadCount))) : 0;
var pageCount = threadCount;
for (int j = 0; j < threadCount; j++)
{
var temp = data.Skip(j * pageSize).Take(pageSize).ToList();
var tempLength = temp?.Count;
Parallel.ForEach(temp, item =>
{
result.Add(item.ID, null);
//call Api and get resultApi
if (resultApi != null && resultApi.Result != null)
{
result[item.ID] = resultApi.Result.id;
}
else if (resultApi != null && resultApi .Message != null)
{
result[item.ID] = null;
}
else
{
result[item.ID] = null;
}
});
}
problem
in end operation in top when check result i see some items are not related to their ID and have been moved.If there is no shift when I exit the parallel mode, all the identifiers are set correctly
how resolve problem?