C# Timer and running method in other thread

Hi,

I am running a timer in C# component with some basic boolean intersetion method.

When i try to run this method in other thread:

            //System.Threading.Tasks.Task.Run(() => {
                this.taskIsFinished = false;
                Brep result = RhinoGeometry.BrepUtil.Iterate2(ref brepTree, ref splitterTree, ref interTree, n, extras);
                this.brepCutTree.Add(result, brepTree.Paths[n]);
                n++;
                this.taskIsFinished = true;
            //});

The component keeps constantly running this line of code without stopping:
control.Invoke(action, true); in TimerElapsed method.

Without threading the program stops when all booleans are finished, but with thread it keeps expiring the component.

How can I correctly run Iterate2 method in a separate thread?
The full code is attached here:
BooleanBrep.cs (5.9 KB)

Are you already aware of

In your example, if I pass breps to the SolveResults function would it be thread safe?

It seems it speeds up polysurface boolean intersection at least two times, awesome…

However I have issue when retrieving data using code below. It seems that GetSolveResults returns 2-3 times the same breps then skips 2-3 elements and returns others. SolveResults functions does work correctly but outputting data is not good.

It seems I do not get fully the example code.
I would like to run the same function multiple times, while example shows only one method.
I also sometimes have duplicate results when running in parallel.

                System.Threading.Tasks.Task<SolveResults> task = System.Threading.Tasks.Task.Run(() => ComputeIntersection(brep, splitters, inter, extras), CancelToken);
                TaskList.Add(task);

                GetSolveResults(DA, out SolveResults result);

                if(result.Value != null) {
                    this.brepCutTree.Add(result.Value, brepTree.Paths[n]);
                    n++;
                }

@stevebaer knows this code more intimately, he’ll be of better help here.