algorithmic modeling for Rhino
Dear all,
I am beginning to play with multi-threading but my understanding of it still being limited, I am facing a problem with the ExpireSolution method. What I am trying to achieve is quite simple: I am triggering a loop on a separate thread so that I can harvest some results, then when a new result is available (in the reality it can take some time) I try to expire the component and to trigger a new solution to update its output.
My problem is that the only way I found to get it to work right now is to use a timer to refresh the component so that it checks if the background task is finished at each interval, but it is a very unpleasant solution since it's forcing the solution of the component everytime. This is why I am trying to use BeginInvoke and Callbacks, as mentioned in older discussions that I found in the forum.
But I suppose I am not doing something correctly, because I still end up triggering solutions when I am not supposed to, and turning everything in ultra-paranoid mode.
I attached a simple vb component that shows the problem.
It's working as follows:
In SolveInstance
If Start Then
If MyghDoc Is Nothing Then MyghDoc = owner.OnPingDocument
Dim RunDelegate As AsyncDelegate = AddressOf DoSomeStuff
RunDelegate.BeginInvoke(AddressOf Callback, Nothing)
End If
If Reset Then Counter2 = 0
A = Counter2
Outside SolveInstance
Public Delegate Sub AsyncDelegate()
Dim Counter As New Integer
Dim Counter2 As New Integer
Public MyghDoc As GH_Document
Public Sub DoSomeStuff()
While Counter < 10
Counter += 1
Threading.Thread.Sleep(10)
End While
Counter = 0
Counter2 += 1
End Sub
Public Sub Callback(ByVal ar As IAsyncResult)
If MyghDoc.SolutionState = 0 Then
owner.ExpireSolution(True)
Else
owner.ExpireSolution(False)
myghdoc.ScheduleSolution(1)
End If
End Sub
If you start the component and try to pan the canvas, it will force the drawing pipeline to refresh and will trigger the error I am mentioning.
So if some of you have a clue about how to make it thread safe without refreshing all the time... thanks in advance for the help.
T.
Tags:
Anyone?
There's a number of weird things. I'll make some modifications and test them. I think you misunderstood some things about threads, asynchronicity and callbacks.
--
David Rutten
david@mcneel.com
Tirol, Austria
I wrote a proof-of-concept script that differs a bit from yours.
The script spawns a low-priority background thread which continually checks for the existence of a file (hardcoded to C:\testfile.txt). As soon as it finds this file exists, it will place the contents of that file into a local variable, trigger the ExpireSolution method on the main Rhino UI thread and then terminate the background thread.
Basically every time you write a new file to that location, it will get read and deleted sometime within 50ms.
--
David Rutten
david@mcneel.com
Tirol, Austria
Thank you for your help David, the invoke stuff was a bit tricky but now with your example it is more clear.
Thanks again!
Welcome to
Grasshopper
© 2025 Created by Scott Davidson.
Powered by