Slow WMI-Query, what reason?
-
Hi :) I tried to get some printer-informations by using a wmi-query. It works fine until a printer goes offline. It takes very long to get those informations then. What do i want it to do? - It should be much faster to get the printer-informations (about 3 secs, no matter whether a printer is offline or not) OR - It should run the application.doevents() command at least every second. (to provide a progressbar which is worth to be called a PROGRESSbar ;) What did i already try to do? - Optimize the WMI-Query (which was a bit successful at least) - Set a seperate Timer which runs the application.doevents() command (for the possibility of a progressbar, which shows that the computer doesn't hang around) The Query-optimization increased the speed by 1-2 seconds. but it still took too long to get the printer-informations. The timer freezes if the specific line of code is executed (if all printers are online its about 3secs, if only one of the printers is offline, it takes up to 15 secs and more), so that's not the solution, too. All together: very unsuccessful I will show you some example code, so you may rebuild the problem, if you're interested in ;) Declaration/Definition of variables:
Private Printer As System.Management.ManagementObject Private PrinterSelect As New Management.SelectQuery("SELECT Name, PrinterStatus, DriverName, PortName, Comment, Local, Network, Default FROM Win32_Printer") Private PrinterSummary As New Management.ManagementObjectSearcher(PrinterSelect) Private PrinterCollection As Management.ManagementObjectCollection
executing code:PrinterCollection = PrinterSummary.Get() 'Application.DoEvents() For Each Printer In PrinterCollection {...} Next Printer PrinterCollection = Nothing
everytime the third line of executing code ("For Each Printer in PrinterCollection") being executed, the computer starts to hang. please tell me, if you need more informations about the problem. i would be thankful and pleased for every constructive response :) so far, mik -
Hi :) I tried to get some printer-informations by using a wmi-query. It works fine until a printer goes offline. It takes very long to get those informations then. What do i want it to do? - It should be much faster to get the printer-informations (about 3 secs, no matter whether a printer is offline or not) OR - It should run the application.doevents() command at least every second. (to provide a progressbar which is worth to be called a PROGRESSbar ;) What did i already try to do? - Optimize the WMI-Query (which was a bit successful at least) - Set a seperate Timer which runs the application.doevents() command (for the possibility of a progressbar, which shows that the computer doesn't hang around) The Query-optimization increased the speed by 1-2 seconds. but it still took too long to get the printer-informations. The timer freezes if the specific line of code is executed (if all printers are online its about 3secs, if only one of the printers is offline, it takes up to 15 secs and more), so that's not the solution, too. All together: very unsuccessful I will show you some example code, so you may rebuild the problem, if you're interested in ;) Declaration/Definition of variables:
Private Printer As System.Management.ManagementObject Private PrinterSelect As New Management.SelectQuery("SELECT Name, PrinterStatus, DriverName, PortName, Comment, Local, Network, Default FROM Win32_Printer") Private PrinterSummary As New Management.ManagementObjectSearcher(PrinterSelect) Private PrinterCollection As Management.ManagementObjectCollection
executing code:PrinterCollection = PrinterSummary.Get() 'Application.DoEvents() For Each Printer In PrinterCollection {...} Next Printer PrinterCollection = Nothing
everytime the third line of executing code ("For Each Printer in PrinterCollection") being executed, the computer starts to hang. please tell me, if you need more informations about the problem. i would be thankful and pleased for every constructive response :) so far, mikWMI is slow, no fixing that.. But to you could code it this way, it will help with the "hanging": http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemmanagementmanagementoperationobserverclasstopic.asp[^] hope this helps, progload
-
WMI is slow, no fixing that.. But to you could code it this way, it will help with the "hanging": http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemmanagementmanagementoperationobserverclasstopic.asp[^] hope this helps, progload
Great. It works fine. Now i'm able to make the progressbar be a real progressbar which shows continously progress ;) thanks a lot, wouldn't have get that by myself. here's some example code how it could look like: declaretion/definition: Private Printer As System.Management.ManagementObject Private PrinterSelect As New Management.SelectQuery("SELECT Name, PrinterStatus, DriverName, PortName, Comment, Local, Network, Default FROM Win32_Printer") Private PrinterSummary As New Management.ManagementObjectSearcher(PrinterSelect) Private PrinterCollection As Management.ManagementObjectCollection Private observer As New Management.ManagementOperationObserver Private completionHandler As New WMICompletionHandler executing code: PrinterCollection = Nothing PrinterSummary.Get(observer) PrinterCollection = PrinterSummary.Get() While Not (completionHandler.IsComplete) Application.DoEvents() System.Threading.Thread.Sleep(500) End While For Each Printer In PrinterCollection {...} next printer ... it redraws the progressbar while executing the wmi query, too. you need to add the completionhandler class, shown on that msdn page. thanks again :)