Is it possible to run ReferenceIntersector.FindNearest in a parallel for loop and avoid crash

Anonymous

Is it possible to run ReferenceIntersector.FindNearest in a parallel for loop and avoid crash

Anonymous
Not applicable

Hello I tried to run ReferenceIntersector.FindNearest in a parallel for loop but revit always crash  I would like to know if it is possible to run ReferenceIntersector.FindNearest in a parallel for loop and here is my normal for loop

 

ReferenceIntersector refIntersector = new ReferenceIntersector(IdFilter, FindReferenceTarget.Face, Get3DView(doc) as View3D);
for (int i = 0; i < points.Count; i++)
{
ReferenceWithContext referenceWithContext = refIntersector.FindNearest(points[i].OriginalPoint, points[i].OriginalPointNormal);
}

 

and here is my parallel for loop that crashes revit.

 

Parallel.For(0, points.Count , i =>
{

ReferenceWithContext referenceWithContext = refIntersector.FindNearest(points[i].OriginalPoint, points[i].OriginalPointNormal);
});

 

 

0 Likes
Reply
425 Views
2 Replies
Replies (2)

RPTHOMAS108
Mentor
Mentor

The API objects are likely not thread safe since it is well established interaction with Revit via API is single threaded. You can often start separate threads in API but not interact with Revit in a multi threaded way.

 

You also may find your error is not only to do with TPL. Generally I would yield the results of a deferred execution to list before calling API functions on those objects within a loop.

 

0 Likes

zhuliyi0
Enthusiast
Enthusiast

Would you consider using some third-party geometry library? It's definitely thread-safe. I have been using geometry 3 sharp BTW.

0 Likes