InvokeOnMainUiThread


#1

I have created a script that set up an UI in the MainTread and listens to a TCPIP server with a TimerThread.
On certain TCPIP commands I have to come back to the MainTread and execute a Rhino-Command (rs.Command("_-Import _Enter"))
This did not work so I have prepared a simple example (please have a look to code below)

In this example a TimerThread is started.
The TimerThread executes the Rhino function InvokeOnMainUiThread.
This function does not work properly, since the referenced function is executed in the TimerThread and not in the MainThread.

Please have a look to my example and drop me a line why function does not work.
Maybe there is a work around?

# -*- coding: utf-8 -*-
"""
Created on Thu Aug 20 14:31:27 2015
Filename: Z3D_Inspect_Scan_01
@author: Daniel Härter, Z-LASER
"""
import rhinoscriptsyntax as rs
import Rhino
import clr, System, threading, time

#============================= MAIN LOOP ===========================
def Main():
    print('Thread Name of Main Function = '+str(threading.current_thread().name))
    # Timer thread
    timerThread = threading.Thread(target=thread_loop)
    timerThread.daemon = True
    timerThread.start()
    #
    print('Main exit')


#============================= Test Function ===========================
def test():
    print('Test Function')
    print('Thread Name of Test Function = '+str(threading.current_thread().name))

clr.AddReference('IronPython')
from IronPython.Compiler import CallTarget0
delegate = CallTarget0(test)


#============================= Thread Loop ===========================
def thread_loop():
    for i in range(3):
        time.sleep(1)
        print('Run thread '+str(i))
        Rhino.Runtime.HostUtils.InvokeOnMainUiThread(delegate)


# Execute it...
if( __name__ == "__main__" ):
    Main()

(Dale Fugier) #4

Hi Danial,

It’s not clear to me that InvokeOnMainUiThread works, or at least works how you want.

Have you tried this in the Rhino WIP?

– Dale


#5

Dear Dale,

thank You for the quick answer.
I have attached my demo script and here is the output:

Thread Name of Main Function = MainThread
Main exit
Run thread 0
Test Function
Thread Name of Test Function = Thread-1
Run thread 1
Test Function
Thread Name of Test Function = Thread-1
Run thread 2
Test Function
Thread Name of Test Function = Thread-1

According to the manual the function “InvokeOnMainUiThread” executes a function in the main ui thread (Calls a method on the main Rhino UI thread if this is necessary.)
This is fundamental, because in every other thread the Rhino command function (rs.Command("_-Import C:\work\1.stl _Enter") does not work.

I use ”InvokeOnMainUiThread” to call the function “test” in the demo script.
Inside the test function I print out the name of the actual thread which is “Thread-1” and not “MainThread”.
So “InvokeOnMainUiThread” does not as expected in my opinion?

Best regards,
Daniel

Issue01.py (1.14 KB)


(Steve Baer) #7

InvokeOnMainUiThread does not exist in the Rhino WIP and I don’t believe it worked in V5. This has been replaced by a InvokeOnUiThread function on RhinoApp in the WIP.

If you need this functionality in V5, you should create a window and call Invoke on it with your callback.


#8

Dear Steve,

What is the Rhino WIP?
I find the function “InvokeOnMainUiThread” in the Rhino context “Rhino.Runtime.HostUtils“ (see picture below).
I do not find “InvokeOnUiThread”.

I already have created a window and tried to call Invoke on it with a callback but this did not work.
This is why I try “InvokeOnMainUiThread”.

Do You have an example?

Best regards,
Daniel

[/uploads/default/original/3X/4/9/491c407464bff2408349a4c13e4e387818f7d5c0.jpg]


(Wim Dekeyser) #9

The Rhino WIP - in this context - is the Work-in-Progress version of Rhino 6.
When you read discourse in your browser, you should see this banner at the top:

That link will give you a download link.
Note that Rhino 5 will no longer be updated at this point…