Просмотр исходного кода

Fix call_on_qt_thread decorator

CURA-6225

Do thread check in the wrapper function, not outside.
Lipu Fei 6 лет назад
Родитель
Сommit
f521fae152
2 измененных файлов с 13 добавлено и 2 удалено
  1. 8 1
      cura/Utils/Threading.py
  2. 5 1
      plugins/UFPWriter/UFPWriter.py

+ 8 - 1
cura/Utils/Threading.py

@@ -1,3 +1,4 @@
+import functools
 import threading
 
 from cura.CuraApplication import CuraApplication
@@ -6,7 +7,7 @@ from cura.CuraApplication import CuraApplication
 #
 # HACK:
 #
-# In project loading, when override the existing machine is selected, the stacks and containers that are correctly
+# In project loading, when override the existing machine is selected, the stacks and containers that are currently
 # active in the system will be overridden at runtime. Because the project loading is done in a different thread than
 # the Qt thread, something else can kick in the middle of the process. One of them is the rendering. It will access
 # the current stacks and container, which have not completely been updated yet, so Cura will crash in this case.
@@ -22,7 +23,13 @@ class InterCallObject:
 
 
 def call_on_qt_thread(func):
+    @functools.wraps(func)
     def _call_on_qt_thread_wrapper(*args, **kwargs):
+        # If the current thread is the main thread, which is the Qt thread, directly call the function.
+        current_thread = threading.current_thread()
+        if isinstance(current_thread, threading._MainThread):
+            return func(*args, **kwargs)
+
         def _handle_call(ico, *args, **kwargs):
             ico.result = func(*args, **kwargs)
             ico.finish_event.set()

+ 5 - 1
plugins/UFPWriter/UFPWriter.py

@@ -44,8 +44,12 @@ class UFPWriter(MeshWriter):
     # trigger loading other containers. Because those loaded containers are QtObjects, they must be created on the
     # Qt thread. The File read/write operations right now are executed on separated threads because they are scheduled
     # by the Job class.
-    @call_on_qt_thread
     def write(self, stream, nodes, mode = MeshWriter.OutputMode.BinaryMode):
+        print("------------>   self _write = ", self._write)
+        return self._write(stream, nodes, mode = mode)
+
+    @call_on_qt_thread
+    def _write(self, stream, nodes, mode = MeshWriter.OutputMode.BinaryMode):
         archive = VirtualFile()
         archive.openStream(stream, "application/x-ufp", OpenMode.WriteOnly)