0

I'm trying to run two functions in parallel with the following code but the GUI stops responding and then crashes.

Function 1 : Downloads a files, it takes like 2-3 minutes to do it.

Function 2 : Updates a progress bar base on a timer and if the function 1 emits an event it brakes from the while loop and sets the progress bar to 100% and re-enable some buttons.

Here is the sample code:

def run_functions_in_parallel(self):
    e = threading.Event()
    t1 = threading.Thread(target=self.Download_dummy_video, args=(e,))
    t2 = threading.Thread(target=self.update_progressbar_with_timer, args=(e,))
    t1.start()
    t2.start()

def update_progressbar_with_timer(self, e):
    from datetime import datetime
    start_time = datetime.now()
    procent_time = 0
    self.disable_button()   # disable buttons while function running
    while True:
        time_diff = datetime.now() - start_time
        if time_diff.seconds > 10:
            start_time = datetime.now()
            procent_time = procent_time + 1
            self.ui.progressBar.setProperty("value", round(((procent_time + 1) / 30) * 100))
            QGuiApplication.processEvents()
            if e.isSet():
                self.ui.progressBar.setProperty("value", 100)
                QGuiApplication.processEvents()
                self.set_back_buttons_to_enable()
                self.ui.status_lbl.setText("Video downloaded...")
                QGuiApplication.processEvents()
                break

def Download_dummy_video(self, e):
    try:
        Download_dummy_video()
        e.set()  # sets when DUMMY video is downloaded
    except:
        print("Something went wrong...")
mike_aze
  • 15
  • 5

0 Answers0