Wait for NewSolution to finish


I need to iteratively start a doc.NewSolution for an optimization but I need to wait for the new solution to finish executing before resuming my program, or the new solutions will start to get all mixed up. I was wondering what would be the best way to achieve that? I’m having some trouble understanding how the new solution process works and how to handle that… Any advice will be incredibly helpful.



Hello Dani,

Thank you for commenting but doesn’t Schedule Solution wait before executing the new solutions? Doesn’t the rest of the program continue executing while the new solution is scheduled to be executed? Also, Schedule Solution erases new scheduled solutions if another solution is triggered before the schedule is executed, which I think would be a problem in a fast iterative process. I don’t see how Schedule Solution would help in my case… I tried it before and it didn’t seem to work… Is there something that I’m missing?

Hard to help here without a code example that fails.

I think schedule solution waits until a curent solution is finished. If that happens, there is a conceptual problem with what you’re trying to do anyways…
In my opinion, the question here is not about schedule solution vs. new solution (personally i also used the former for an optimization component), it’s the code you are executing those command sfrom, how to make sure that your optimization loop triggers the solution and waits for the solver to be finished. And I think the bigger problem is the thread safety of Grasshopper.
There are a couple of threads on the forum where this has been discussed as well, maybe start with those…

Hello @dsonntag,

I did see several threads discussing the same thing (many of them yours and Thomas Wortmann) and I’m facing exactly the same problems as you have faced in the past. I’m running an optimization in a Backgroundworker and I have to access Grasshopper sliders to adjust the values and wait for the solution to refresh to collect the fitness.

I have tried Thomas Wortmann’s approach in his FroG application, as well as your approach here and although I can see the sliders moving (I am succesfully changing the sliders), the fitness I’m collecting does not correspond to the values set in the sliders. I think this means the fitness collected is out of Sync with the current active solution?

Mostly I’m just trying to undertand how the NewSolution/ScheduleSolution work and how do I know that a new solution has finished executing.


Together with what you quoted, I have something like this in my code, but it’s been a while since I wrote this, so I don’t recall all the details. In the form where all this is running there is a field:

private AutoResetEvent waitHandle;

and at some point during construction I subscribe to the event.

mainDocument.SolutionEnd += new GH_Document.SolutionEndEventHandler(OnSolutionEnd);
waitHandle = new AutoresetEvent(false);

The definition of the EvendHandler:

private void OnSolutionEnd(object sender, GH_SolutionEventArgs e)

And the AutoResetEvent needs to be Reset before the optimization starts. So you schedule the solution, then waitHandle.WaitOne() which freezes this thread until the solution is over, which triggers the SolutionEnd event which sets the AutoResetEvent so that the other thread can continue running (and collect results from the component).

1 Like

That looks like it could work! I’ve been trying to figure out how to use the Solution End Event for this purpose, until now unsuccesfully. Let me try that approach and I’ll get back to you.

Hello @dsonntag,

After some reorganizing and code cleaning, I tried your approach and it worked! It does seem like a better idea to use the native Solution End event for this. Thank you so much for your help!!