File tree Expand file tree Collapse file tree 1 file changed +9
-5
lines changed
screen_brightness_control Expand file tree Collapse file tree 1 file changed +9
-5
lines changed Original file line number Diff line number Diff line change 11import logging
22import re
3- import threading
43import time
54from contextlib import contextmanager
65from ctypes import Structure , WinError , byref , windll
3938@contextmanager
4039def _wmi_init ():
4140 '''internal function to create and return a wmi instance'''
42- # WMI calls don't work in new threads so we have to run this check
43- com_init = threading .current_thread () != threading .main_thread ()
44- if com_init :
41+ com_init = False
42+ try :
43+ yield wmi .WMI (namespace = 'wmi' )
44+ except Exception as e :
45+ # WMI init will fail outside the main thread, or if CoInitialize wasn't called first
46+ _logger .debug (f'WMI init failed ({ e !r} ). Calling CoInitialize and retrying' )
47+ com_init = True
4548 if COM_MODEL is None :
4649 pythoncom .CoInitialize ()
4750 else :
4851 pythoncom .CoInitializeEx (COM_MODEL )
4952
50- yield wmi .WMI (namespace = 'wmi' )
53+ yield wmi .WMI (namespace = 'wmi' )
5154
55+ # only uninitialise if we initialised. Avoid cleaning up resources being used by another library
5256 if com_init :
5357 pythoncom .CoUninitialize ()
5458
You can’t perform that action at this time.
0 commit comments