Behrooz
Tahanzadeh


Expiring solution in Grasshopper

Regarding Bengesht 3.2.0.0!

Due to sequential model of Grasshopper it’s tricky to implement asynchronized codes. The idea is that you have to focus on single function called SolveInstance to read, calculate and export the results. But when it comes to handling threads it means that all of states of the code have to be handled through same function and one of these states is exporting data!

Working on Bengesht Websocket functionality I faced this problem that the receiver component had to expire solution whenever server is sending back a message. (Asynchronized)  After releasing few buggy version of Bengesht (SORRY!) finally I sat down and looked at the UDP Receiver component of GhOwl (Thanks OpenSource). In conclusion these items have to be considered in similar cases:

 

doc.SolutionState != GH_ProcessStep.Process

It’s clear enough! Never expire a solution while previous solution is still processing.

 

GH_InstanceServer.DocumentEditor.BeginInvoke

This function calls an action through main thread of Grasshopper. It will avoid conflict between running threads that want to access main thread. This method works stable however it’s been obsoleted. The replaced way is to use ScheduleSolution.

 

doc.ScheduleSolution

If you want to read in detail about this function I suggest to follow this discussion in Grasshopper forum. The short version is that this function while run the callback function after a specific delay. It’s quite straightforward. Also keep in mind that to avoid recalculating solution pass false in ExpireSolution function. This will mark the current component as EXPIRED but the document will be recompute but ScheduleSolution function.

ghDocument.ScheduleSolution(0, doc => {
this.ExpireSolution(false);
});