I’m performing some element UI hit testing like this:
internal MyElement[] HitTest((double x, double y) point, double scale)
{
ConcurrentBag<MyElement> geoms = new ConcurrentBag<MyElement>();
var point = new Point { X = point.x, Y = point.y };
Parallel.For(0,Output.Length, (d) =>
{
var element = UIElements[d];
if (element is IHitTestable ht && ht.HitTest(ref point,scale) geoms.Add(element);
});
return geoms.ToArray();
}
I would like to refactor this to remove closures around local variables; also I believe that each iteration of the loop might be allocating anonymous stuff (classes,methods) per each iteration.
The issue is that the Parallel.For()
parameters expect an Action<int>
as a delegate to run in parallel, whereas I need a delegate Action<int,Point,double>
to handle the local variables so that I can write a compile-time method with the expected argument types.
How do I provide Parallel.For()
with an appropriate delegate without anonymity?