albertosottile / darkdetect

Detect OS Dark Mode from Python
Other
171 stars 18 forks source link

A way to terminate listener thread #29

Closed demberto closed 1 year ago

demberto commented 1 year ago

Current Windows implementation doesn't allow breaking out of the infinite while loop

albertosottile commented 1 year ago

Hello, I am not sure I understand your question. The listener is meant to be used in a separate thread. You should then be in control of this separate thread and you can then freely terminate it at any time. The listeners implemented up to now do not need to be "terminated gracefully", if this is what you are asking.

5yutan5 commented 1 year ago

Hi, If large performance decrease with this thread, I think it would be better to be able to terminate a thread when don't need to listen to the changes to the OS's theme.

demberto commented 1 year ago

Hi, If large performance decrease with this thread, I think it would be better to be able to terminate a thread when don't need to listen to the changes to the OS's theme.

The current implementation uses the synchronous version of RegNotifyChangeKeyValue. So it blocks the thread until the value is changed. So the only performance hit is whenever the theme actually changes. No polling is done.

demberto commented 1 year ago

The listener is meant to be used in a separate thread. You should then be in control of this separate thread and you can then freely terminate it at any time. The listeners implemented up to now do not need to be "terminated gracefully", if this is what you are asking.

I am new to threads, it seems discouraged to forcefully stop a thread.

albertosottile commented 1 year ago

I am new to threads, it seems discouraged to forcefully stop a thread.

This is true and by no means I was suggesting to randomly kill threads. What I am saying is that darkdetect only provides functions (listener) that can be used to continuously monitor the GUI settings. It is then left to the user how to use them, e.g. whether they want to encapsulate them in separate threads/processes or run them in the main thread. From our side, the code in the existing implementations of listener can be safely interrupted.

if you wish to encapsulate your listener in an interruptible thread, I would suggest to implement your own thread handling, for example as suggested here: https://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread