简体   繁体   中英

Python timer with Stop Pause and Continue without using time module

How to create Python timer with Stop, Pause and Continue functions without using "time" module, especially time.sleep() because my programm freezes after this function? Thank you

To be more specific what i am trying to acchieve , I want to create an inapplication script that counts for how long was the programm opened. If application was minimized - timer has to pause. In this step, using time.sleep() freezes my application If application is closed - timer has to stop

If I've correctly undestood your problem, you want something to count how long the application is opened, and pausing all the time the application is minimized. For that, you do not need to use any sleep . You simply react to events, noting the time the event occurs and cumulating open time. In pseudo code, it gives :

  • App starts in open mode: cumul = 0, openBeg = now, state = open
  • app starts in minimized mode : cumul = 0, state = min
  • app is minimized : cumul += now - openBeg, state = min
  • app is (re-)opened : openBeg = now, state = open
  • app is closed : if state is open: cumul += now - openBeg

For python, now is time.time() and state should be managed as a boolean opened = True when state is open, opened = False when stat is min.

It is simple if the application calls a similar API, it will be much more tricky if you have to hook on the windows application state changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM