Python Forum
Threading timer crashes my App
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Threading timer crashes my App
#1
I want to make an app that will use timer.
A button will set the timer at my pc 's time plus one minute.
The problem is that it crashes.
Run the following code and press button 'set alarm'.
Then try to close the form clicking the close button ('x') of the form.
It freezes.

I guess I'm not using correctly the threading?

Any good working solution?

import sys
from PyQt4 import QtGui
import os
import threading
import datetime
import pygame

class MyApp(QtGui.QWidget):
   def __init__(self):
       QtGui.QMainWindow.__init__(self)
       pygame.init()
       pygame.mixer.init()
       now = datetime.datetime.now()
       hour = now.hour; minute = now.minute; second = now.second

       # add pusbutton
       self.set_alarm_btn = QtGui.QPushButton('set_alarm_btn', self)
       self.set_alarm_btn.clicked.connect(self._set_alarm_btn_clicked)

       layout = QtGui.QVBoxLayout(self)
       layout.addWidget(self.set_alarm_btn)

   def _set_alarm_btn_clicked(self):
       now = datetime.datetime.now()
       hour = now.hour;
       minute = now.minute;
       second = now.second
       self.clock = Clock()
       self.clock.set_alarm(hour, minute + 1, self)
       self.clock.run()

   def _response_to_alert(args):
       print("Alert")

   def ring_ring(self):
       sys.stdout.write('ring ring\n')
       sys.stdout.flush()
       if  hasattr(self, 'clock'):
           self.clock._alarm_thread.cancel()

class Clock:

   def __init__(self):
       self.alarm_time = None
       self._alarm_thread = None
       self.update_interval = 1
       self.event = threading.Event()

   def run(self):
       while True:
           self.event.wait(self.update_interval)
           if self.event.isSet():
               break
           now = datetime.datetime.now()
           if self._alarm_thread and self._alarm_thread.is_alive():
               alarm_symbol = '+'
           else:
               alarm_symbol = ' '
           sys.stdout.write("\r%02d:%02d:%02d %s"
               % (now.hour, now.minute, now.second, alarm_symbol))
           sys.stdout.flush()

   def set_alarm(self, hour, minute, parent):
       now = datetime.datetime.now()
       alarm = now.replace(hour=int(hour), minute=int(minute))
       delta = int((alarm - now).total_seconds())
       if delta <= 0:
           alarm = alarm.replace(day=alarm.day + 1)
           delta = int((alarm - now).total_seconds())
       if self._alarm_thread:
           self._alarm_thread.cancel()
       self._alarm_thread = threading.Timer(delta, parent.ring_ring)
       self._alarm_thread.daemon = True
       self._alarm_thread.start()


if __name__ == '__main__':
   app = QtGui.QApplication(sys.argv)
   window = MyApp()
   window.show()
   sys.exit(app.exec_())
Reply


Messages In This Thread
Threading timer crashes my App - by panoss - Jun-03-2017, 08:30 AM
RE: Threading timer crashes my App - by Ofnuts - Jun-04-2017, 12:17 PM
RE: Threading timer crashes my App - by metulburr - Jun-04-2017, 05:32 PM
RE: Threading timer crashes my App - by panoss - Jun-05-2017, 03:42 PM
RE: Threading timer crashes my App - by nilamo - Jun-05-2017, 05:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Concurrent futures threading running at same speed as non-threading billykid999 13 1,862 May-03-2023, 08:22 AM
Last Post: billykid999
  Understanding and debugging memory error crashes with python3.10.10 Arkaik 5 2,112 Apr-18-2023, 03:22 AM
Last Post: Larz60+
  using threading.Timer for function korenron 1 1,206 Dec-20-2022, 01:09 PM
Last Post: ndc85430
  Pydroid3 app crashes on xiaomi poco F3 JMD 2 1,257 Nov-27-2022, 11:56 AM
Last Post: JMD
  HTML file crashes program mikefirth 12 3,851 Dec-31-2021, 03:57 AM
Last Post: Pedroski55
  Tutorials on sockets, threading and multi-threading? muzikman 2 2,130 Oct-01-2021, 08:32 PM
Last Post: muzikman
  Scraping a Flexible Element - works at first, and then crashes JonnyB 0 1,517 Aug-14-2021, 07:25 PM
Last Post: JonnyB
  email timer/rss feed timer ndiniz 1 2,088 Feb-02-2021, 07:18 PM
Last Post: nilamo
  Bug that crashes the Python interpreter 3.7 on Mac OS X Stef 1 3,702 Feb-18-2019, 06:30 PM
Last Post: micseydel
  code keeps running if i use from threading import timer? birddseedd 3 2,608 Jan-25-2019, 05:00 AM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020