alogrep 0 Posted May 4, 2023 HI. Is accessing WMI thread safe? I mean this: I my Thread repeatedly list the print jobs in the sn spool with this code FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator'); FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', WbemUser, WbemPassword); FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_PrintJob','WQL',wbemFlagForwardOnly); oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant; etc. ...... Do I need to protect that with a criticalsection Acquire/Release? Thank you Share this post Link to post
PeaShooter_OMO 11 Posted May 4, 2023 (edited) I have used WMI in multiple simultaneous threads before but always created and kept the WMI Objects local to each thread. Are you sure there is no other API/way to do what you want? WMI generally is slow. Edited May 4, 2023 by PeaShooter_OMO Share this post Link to post
alogrep 0 Posted May 4, 2023 Yes, there other ways, but they do not work Anyway, in EACH thread I have a funtcion that does this FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator'); FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', WbemUser, WbemPassword); FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_PrintJob','WQL',wbemFlagForwardOnly); oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant; etc. ...... My question is: do i need to encapsulatwe it in a critical section Acquire/Release?. ( I am rather new to threads, but I would think it is not necessary) Share this post Link to post
programmerdelphi2k 237 Posted May 4, 2023 (edited) @alogrep see that https://social.msdn.microsoft.com/Forums/vstudio/en-US/18866f7c-a896-4465-9208-c920cf1f1158/is-wmi-api-threadsafe-?forum=netfxbcl#:~:text=Your application will be thread,accessing them in different threads. https://microsoft.public.win32.programmer.wmi.narkive.com/vLmbKJcS/wmi-and-multi-threading Edited May 4, 2023 by programmerdelphi2k Share this post Link to post
alogrep 0 Posted May 4, 2023 Thanks. It seems then I do not need to use a critical section because each thread instantiate the object and use the service. Share this post Link to post