I would avoid using rhinoscriptsyntax inside of any multithreaded code. rhinoscriptsyntax directly works with the document and typically adds geometry to the document. Specifically, I can see that rs.TrimBrep
is going to be problematic in the above script.
Use RhinoCommon functions instead