From 95295e1f6198ba6b7ade053ece91fbb58a5493b4 Mon Sep 17 00:00:00 2001 From: Martin Zietz Date: Wed, 10 Feb 2021 11:46:23 +0100 Subject: [PATCH] workaround for thread end problem on program close --- User_Interface.py | 2 +- csv_threading.py | 8 +++++--- main.py | 5 ++++- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/User_Interface.py b/User_Interface.py index 7226c5a..5e7c224 100644 --- a/User_Interface.py +++ b/User_Interface.py @@ -626,7 +626,7 @@ class ExecuteCSVMode(Frame): self.stop_button["state"] = "normal" self.reinit_button["state"] = "disabled" - # g.threadLock = threading.Lock() # create thread locking object, used to ensure all devices switch at once later + g.threadLock = threading.Lock() # create thread locking object, used to ensure all devices switch at once later # create separate thread to run sequence execution in: self.csv_thread = csv.ExecCSVThread("CSV_Thread", self.sequence_array, self, self.controller) self.csv_thread.start() # start thread diff --git a/csv_threading.py b/csv_threading.py index b43556c..ad80021 100644 --- a/csv_threading.py +++ b/csv_threading.py @@ -56,15 +56,17 @@ class ExecCSVThread(Thread): t = time.time() - t_zero # get relative time if t >= array[i, 0]: # time for this row has come - # g.threadLock.acquire(0) + g.threadLock.acquire() # execute the next few lines before going back to the main thread + field_vec = array[i, 1:4] # extract desired field vector - ui.ui_print("%f s: t = %0.2f s, target field vector = " + ui.ui_print("%f s: t = %0.2f s, target field vector = " # ToDo: better printing % (time.time() - t_zero, array[i, 0]), field_vec * 1e6, "\u03BCT") func.set_field(field_vec) # send field vector to test stand ui.ui_print(time.time() - t_zero) controller.StatusDisplay.update_labels() # update status display after change i = i + 1 # next row - # g.threadLock.release() + + g.threadLock.release() # allow going back to main thread now elif t >= array[i, 0] - delay - 0.02: # next change time is close, not enough time to sleep pass diff --git a/main.py b/main.py index 22b374e..14f02e0 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,7 @@ from os.path import exists import traceback from tkinter import messagebox +import time import cage_func as func from User_Interface import HelmholtzGUI @@ -15,7 +16,9 @@ def program_end(): if g.app is not None: if g.app.pages[ui.ExecuteCSVMode].csv_thread is not None: # end possible csv execution thread g.app.pages[ui.ExecuteCSVMode].csv_thread.stop() # stop thread - g.app.pages[ui.ExecuteCSVMode].csv_thread.join() # wait for thread to finish + # g.app.pages[ui.ExecuteCSVMode].csv_thread.join() # wait for thread to finish + # ToDo: figure out why this doesn't work with join() + time.sleep(0.2) # give the thread time to finish, workaround to avoid join() # g.app = None # reset to None so nothing tries to print in the UI output func.shut_down_all() # shut down devices if g.app is not None: