mcneel / compute.rhino3d

REST geometry server based on RhinoCommon and headless Rhino
Other
291 stars 184 forks source link

C# compute client should provide awaitable methods #89

Open pearswj opened 4 years ago

pearswj commented 4 years ago

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().

mcneel-build commented 4 years ago

Linked with COMPUTE-115

DemiChangKPF commented 3 years ago

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;
            }