1

I want to stop a thread if I press a button on my window but I didn't found how to do it, can someone help me ?

I have something like this for the thread class :

class myThread (threading.Thread):

    def __init__(self, source_file, thread_number):

        threading.Thread.__init__(self)

        #self Var#########################

        self.source_file = source_file            
        self.thread_number = thread_number

        ##################################

    def run(self):

        print("Thread"+str(self.thread_number)+" has begun\n\r")

        #Function that will be run########
        #Start Manager
        self.manager.start(self.source_file)

        #Stop Manager
        self.manager.stop(self.log_file)

        print("Thread"+str(self.thread_number)+" has stopped\n\r")

I call a thread when I press on a start button :

print("Thread creation \n\r")

self.thread = myThread(self.source_file, self.thread_number)

self.thread.start()

And I associated a function with my "Stop" button which is :

def stop(self):

        ######################################
        #Function to stop the thread here ...#
        ######################################

        #Stop Manager
        self.manager.stop(self.log_file)

        print("Thread"+str((self.thread_number)-1)+" has stopped in stop()\n\r")

Thank you.

katze
  • 1,273
  • 3
  • 16
  • 24
  • You should read this thread : http://stackoverflow.com/questions/15729498/how-to-start-and-stop-thread. – Luc DUZAN Apr 01 '14 at 16:02
  • Ok thank you I'll read it. Edit: It doesn't solve my problem (unless I don't understand the answer) because I want my button act like an interruption. – katze Apr 01 '14 at 16:14
  • You can't stop a thread. Your only hope is that the code running in the thread has a mechanism to be notified that it should pause or exit. I have no idea what "manager" is, but if it's stop() method is designed to be called from another thread, then you are on the right track. – tdelaney Apr 01 '14 at 16:45

0 Answers0