Optimization Plug-In for GH - UI thread + background worker

OK, so now I reduced the setup, just the backgroundworker with the optimization (as the solver class of the optimization has a pretty clean force stop method which I can also directly call from with my cancel button, no need to have 2 background threads).
After a lot of code changes, finally this seems to work:

  • Lock the UI with a ShowDialog to make sure nobody is playing around with the GH UI while the optimization is running (I think it is pretty weird that you can actually do this in Goat).

  • Encapsulate the interaction with GH UI (where the waitHandle is an AutoResetEvent which get’s set when EndSolution is raised and mainDocument the current GH doc).

          IAsyncResult iAsyncResult = BeginInvoke((MethodInvoker)delegate
          {
              setSliders(x, sliders);   
    
              mainDocument.ScheduleSolution(1);
          });
          this.waitHandle.WaitOne();
          EndInvoke(iAsyncResult);
    

Some things remain still pretty unclear to me, if anybody has some thoughts on that, I’d appreciate it:

  • When I called mainDocument.newSolution(false) it raises at least two times the SolutionEnd event, no idea why that is, that’s why I stuck with scheduleSolution(1)
  • The backgroundworker_RunWorkerCompleted also claims a cross-thread violation when I manipulate the UI from it, which doesn’t make sense to me, as it should also be called on the UI (as the backgroundworker was also created on the UI thread).

Besided, the FroG hat some useful code snippets (e.g. the slider manipulations), so thanks for that.

2 Likes