Quantcast
Channel: Scripting - McNeel Forum
Viewing all articles
Browse latest Browse all 5804

InvokeOnMainUiThread

$
0
0

@haerter wrote:

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()

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 5804

Trending Articles