compute.rhino3d icon indicating copy to clipboard operation
compute.rhino3d copied to clipboard

C# compute client should provide awaitable methods

Open pearswj opened this issue 4 years ago • 2 comments

Some functionality could take a while to solve and it would be nice if the C# client included awaitable methods to simplify backgrounding out of the box.

async void DoStuff(IEnumerable<Brep> breps)
{
  var union = await BrepCompute.CreateBooleanUnion(breps);
  // do more stuff
}

// or

var task = BrepCompute.CreateBooleanUnion(breps);
while (!task.Wait(250))
{
  // keep ui alive
  // check timeout
}
var res = task.Result;

We might need another namespace or an additional set of classes/methods, so as not to mess with anyone currently using the synchronous methods, e.g. using Rhino.ComputeAsync; or BrepComputeAsync.CreateBooleanUnion() or BrepCompute.CreateBooleanUnionAsync().

pearswj avatar May 30 '20 07:05 pearswj

Linked with COMPUTE-115

mcneel-build avatar May 30 '20 07:05 mcneel-build

A solution for running compute API calls without freezing rhino (this example calls EvaluateDefinition for grasshopper, but could work with any other call) :

// make api call
            List<GrasshopperDataTree> output;
            try
            {
                var asyncoutput = Task.Run(()=>EvaluateDefinition(grasshopperFile, trees));
                while (! asyncoutput.IsCompleted) // while Task is not yet completed
                {
                    Rhino.RhinoApp.Wait(); // Rhino keeps running while waiting
                }
                output = asyncoutput.Result; // gets result
            }
            catch (AggregateException ae) // exceptions happen inside Task
            {
                ae.Handle((x) =>
                {
                    if (x is System.Net.WebException) 
                    {
                        // handle error
                        return true;
                    }
                    return false; 
                });
                return;
            }

DemiChangKPF avatar Oct 23 '20 16:10 DemiChangKPF