Hi folks,

my threads is running well.
But I would like to implement terminating of thread. I have two approaches
but any of these is not working properly till now.

First approach:
1) class contain function: run_function()
2) over button I call:
        self.thread = threading.Thread(target=self.run_function()
        self.stop = threading.Event()
3) over stop button I would like to cancel thread. In that function is written:
        if self.thread.isAlive():
            self.stop.set()
            self.thread.join()
This approach unfortunatelly does not work.

Second approach:
class DevAssistantThread(threading.Thread):
    def __init__(self, target = None):
        threading.Thread.__init__(self)
        self._terminate = False
        self.target = target
        self.stop = threading.Event()

    def terminate(self):
        print "terminate is called"
        self._terminate = True
        self.stop.set()
        print "terminate is finished"

    def run(self):
        print "Function was run"
        self.target()
        print "Function was finished"
        while True:
            if self._terminate:
                print "Thread is canceled"
                break

class runWindow(object):
    def __init__(self,  parent, finalWindow, builder, assistant):
        self.thread = DevAssistantThread(target=self.devassistant_start)

    # Thread is started during the opening window
    def open_window(self, widget, data=None):
        dirname, projectname = self.parent.pathWindow.get_data()
        self.runWindow.show_all()
        self.cancelBtn.set_sensitive(False)
        self.thread.start()
        self.cancelBtn.set_sensitive(True)

    # Function for cancelling thread
    def close_btn(self, widget, data=None):
        name = self.cancelBtn.get_label()
        if name == "Cancel":
            print self.thread.isAlive()
            if self.thread.isAlive():
                Gdk.threads_enter()
                self.stop.set()
                self.thread.join()
                Gdk.threads_leave()
            Gtk.main_quit()
        else:
            print "Quit dialog"
            Gtk.main_quit()
    def devassistant_start(self):
        logger_gui.info("Thread run")
path = self.assistant.get_selected_subassistant_path(**self.parent.kwargs)
        pr = path_runner.PathRunner(path, self.parent.kwargs)
        try:
            pr.run()
            Gdk.threads_enter()
            self.cancelBtn.set_label("Close")
            Gdk.threads_leave()
        except exceptions.ExecutionException as ex:
            pass

Unfortunatelly also second approach is not working well. Respectivelli it does not work at all.

Do you have any idea where I have made mistake?

Thank you in advance

--
Best regards / S pozdravem
Petr Hracek

_______________________________________________
pygtk mailing list   pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/

Reply via email to