-1

I have this class which is my thread. By creating instance of the class I create my thread, so by calling its stop method it should be terminated or killed but it wouldn't. I don't know why? I appreciate if someone can help me.

class MyThread(threading.Thread):

# Thread class with a _stop() method.

# The thread itself has to check

# regularly for the stopped() condition.

def __init__(self, last_time=0, start_time=0, label="", root="",  speed_mode=""):

    super(MyThread, self).__init__()
    self.label = label
    self.last_time = last_time
    self.start_time = start_time
    self.root = root
    self.speed_mode = speed_mode
    self._stop_event = threading.Event()

    # function using _stop function

def start_counter(self):
    self.start_time += 1
    self.label['text'] = self.start_time
    # self.start_time = start_time
    if self.start_time < self.last_time:
        self.label.after(DIC_TIME[self.speed_mode], self.start_counter)

def stop(self):

    self._stop_event.set()

def stopped(self):

    return self._stop_event.isSet()

def run(self):
    self.start_counter()
    while True:
        if self.stopped():
            return

I expect that by calling stop method of MyThread class my label in tinter canvas restart and stop running(label is a second counter)

Arash
  • 49
  • 7
  • It is never a good idea for one thread to kill a thread. It is never a good idea for one thread to do _anything_ to another thread. Threads should _cooperate_ with one another. If thread A knows that thread B is no longer needed, then thread A should _ask_ thread B to gracefully shut itself down, and thread B should be designed to respond promptly and comply with the request. – Solomon Slow Apr 10 '19 at 14:03

1 Answers1

0

Try this: Is there any way to kill a Thread? hopefully it will be what you`re looking for, hopefully it's what you're looking for

Octavian
  • 505
  • 2
  • 4
  • 16