I have a list of nodes in a tree list.
At times I turn of the sorting on columns and in order to keep the same structure/position of the nodes I copy the structure as is, put it in a list and when the deactivation of the sort is done I put back the nodie positions. This is done like this:
List<TreeListNode> nodes = new List<TreeListNode>();
nodes.AddRange(xtlItemList.Nodes);
// deactivete sorting
foreach (var c in xtlItemList.Columns) {
c.SortOrder = SortOrder.None;
}
// put back node positions
for (int i = 0; i < nodes.Count; i++)
{
xtlItemList.SetNodeIndex(nodes[i], i);
}
The problem:
Is that the second loop take alot of time to execute. For 1043 nodes it takes up to 50 seconds.
I thought I could optimize it via a parallel for loop:
int counter = nodes.Count -1;
try
{
Parallel.For
(0
, counter
, new ParallelOptions { MaxDegreeOfParallelism = 5 }
, (i) =>
{
try
{
xtlItemList.SetNodeIndex(nodes[i], i);
}
catch (Exception exception)
{
//throw;
}
}
);
}
catch (Exception exx)
{
}
I am getting some weird result. most of the time the nodes in the ui disappear and at time I am getting a null reference exception which confuses me even more.
What am I missing here?