every time when I close the GuiWindow, I found the thread not be stoped, when I run my script again, always crash.
Anyone can tell me how to join the threads when I close the GuiWindow?
- Code: Select all
# Clarisse socket client
import ix
app = ix.application
import socket,threading,time
class ML_BridgeClient():
def __init__(self):
#super(ML_BridgeClient, self).__init__()
try:
self.startThread()
except:
pass
def startClient(self):
HOST = '127.0.0.1'
PORT = 37373
try:
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((HOST, PORT))
connect_label.set_label("Connected!")
window.redraw()
client.sendall(b'Clarisse')
while True:
data = client.recv(2048)
if data:
print('Received', repr(data))
client.close()
except:
connect_label.set_label("Missing connection, trying again...")
window.redraw()
time.sleep(2)
self.startClient()
def startThread(self):
self.t = threading.Thread(target=self.startClient)
self.t.start()
#<<<<<<<<<<<<<<<<<<<<<<<<<<< UI
version = "1.0"
app_x = ix.application.get_event_window().get_position()[0]
app_y = ix.application.get_event_window().get_position()[1]
app_w = ix.application.get_event_window().get_width()
app_h = ix.application.get_event_window().get_height()
window_x = 350
window_y = 80
clarisse_win = ix.application.get_event_window()
window = ix.api.GuiWindow(clarisse_win, app_x+(app_w-window_x)/2, app_y+(app_h-window_y)/2, window_x, window_y,"Mad Library Bridge v"+version )
window.set_resizable (False)
panel = ix.api.GuiPanel(window,0,0,window_x,window_y)
connect_label = ix.api.GuiLabel(panel,20,20,360,40,"Trying to connect...")
print("Tring to connect to server...")
bridge = ML_BridgeClient()
window.show()
while window.is_shown(): ix.application.check_for_events()