C# Grasshopper Running Timer in a separate thread

Hi,

How could I create a C# timer and run one method in a separate thread at each iteration?

This code runs a boolean intersection multiple times, the process is super slow. Therefore I would like to run it in separate thread, while making rhino responsive.

The full gh code is attached
BooleanInASeparateThread.gh (4.6 MB)

@DavidRutten Is it possible to do this in C# editor or it is only possible in a compiled component?

The code structure is like this:

Component.Message = "Run Boolean";




    //Reset
    if (reset || brepCutTree == null && !bruteForce){
      Clean();



      //Timer
    } else if(run && n < brepTree.BranchCount && !bruteForce){//
      Iterate2(ref brepTree, ref splitterTree, ref interTree, n, Extras);
      n++;
      Component.ExpireSolution(true);



      //Run ball boolean operations at once
    }else if(bruteForce){

      Clean();
      for(int i = 0; i < brepTree.BranchCount; i++){
        Iterate2(ref brepTree, ref splitterTree, ref interTree, i, Extras);
      }

    }


    //Output
    A = brepCutTree;
    B = log;
    C = curves;

Timer code could be:

            Timer timer = new Timer { Interval = 60000 };
            timer.Tick += (sender, e) => TimerElapsed();
            timer.Start();

Some pseudocode:

public async Task<List<WhateverIterate2Returns>> BooleanIntersectionAsync(ref ibrepTree, ref  splitterTree, ref interTree, i, Extras)
 {
            Clean();

            var ints = Enumerable.Range(0, ibrepTree.BranchCount);
            var list = new List<WhateverIterate2Returns>();
            Task.WhenAll(ints.Select(i => list.Add(Iterate2(ref brepTree, ref splitterTree, ref interTree, i, 
            Extras))));

            return list;
}

or

 Parallel.For(0, ibrepTree.BranchCount,
                   i => {  Iterate2(ref brepTree, ref splitterTree, ref interTree, i, Extras););
                   } );

Iā€™m not the most versitale when it comes to parallel/async taks so take that with a grain of salt, but maybe a starting point?

1 Like

Thank you