Jump to content

How to make my GUI responsive during a WMI query?


Recommended Posts

I've written some code that detects the presence of an Arduino-based device plugged into the USB port of a system, and records which COM port number its serial port is running on.

In order to do this, I'm performing a WMI query. This WMI query executes quickly on some systems and slowly on other systems. (The systems where it executes slowly are old hardware test computers that have had a lot different kinds of USB cards plugged into them in the past, thus forcing WMI to enumerate through a fairly large database.)

On the systems where the WMI query executes slowly, the GUI freezes, and I can't even move the main GUI window's position on the screen, until after the query is done. I want to figure out how to make my program's GUI be responsive to user input during this WMI query.

Note: I know how to use the "Return Immediately" flag. However, even when I use the flag, then next line ("IsObj" in this case) will still have to wait until the query is done, so that line is the one where the freeze happens.

Here is a sample snippet of my code. The lines where it freezes are the "ExecQuery" line (if I don't use the 0x10 flag), or the "IsObj" line (if I do use the 0x10 flag).

; Create the WMI object. This works instantly.
   $objWMI = ObjGet("winmgmts:\\.\root\cimv2")
   If IsObj($objWMI) then
           ; Everything is fine, we got a WMI Object
   Else
          ; Everything is not fine. We did not get a WMI object. Make obtrusive error message and bail out of the program.
          UpdateLogFile ("Internal program error. Unable to get handle to WMI object.")
          Exit()
   EndIf

   ; Execute the actual WMI query, specifically looking for the string "Arduino" in the COM port names.
   $objItems = $objWMI.ExecQuery("SELECT * from WIN32_SerialPort WHERE Caption LIKE '%Arduino%'","WQL",0x10+0x20)
   ; Notes on WMI Query:
   ; Common parameters (last parameter at the end there) are, for example:
   ;            0x10+0x20
   ; 0x20 = wbemFlagForwardOnly
   ;           meaning that it saves some memory, at the cost of certain
   ;           functions (such as object.count) being unable to run.
   ; 0x10 = wbemFlagReturnImmediately
   ;           Allows the WMI query to return immediately. This doesn't
   ;           actually help me though! The next thing I do is perform
   ;           an IsObj function... which will freeze my program until such
   ;           time as the query is done. So it doesn't matter if I perform
   ;           the WMI query with the "ReturnImmediately" flag... I'm just
   ;           shifting the freeze from the line above, to the line below.
   ;           either way, on a system with a bunch of WMI objects installed,
   ;           such as a test system that's had a billion cards plugged into
   ;           it before, then the query takes a long time (5 seconds or more)
   ;           during which my program appears to freeze up. I have no idea how
   ;           to get around this freeze... Either "IsObj" locks up my program
   ;           until it finishes, or "ExecQuery" locks up my program until it
   ;           finishes. How do I make my UI responsive during this query?

   If IsObj($objItems) Then
         ; If I have set the wbemFlagReturnImmediately flag, then the line above is the one that freezes the system.
         ; If I do not set the wbemFlagReturnImmediately flag, then the query above it is the one that freezes the system.
         ; I have confirmed this with debug prints.

          ; Ideally we would only get one result. But if we got more than one, loop through them all.
          For $objItem In $objItems
                 $TempString = $objItem.DeviceID
                 ; Whatever the LAST one was that we found, that's what we're going to assume is the Arduino.
                 $ArduinoComPort = Int(StringReplace($TempString,"COM","",0,2))
                 UpdateLogFile("Found Arduino at COM" & $ArduinoComPort & ".")
          Next
   EndIf
Link to comment
Share on other sites

Well i dont see any code of GUI it would be more helpful if u provide the code of GUI

and if u know the line where it freezes try to disable the gui for that particular line and again enable it back (so that it doesnt hangs off)

using

winsetstate('',@SW_DISABLE)
winsetstate('',@SW_ENABLE)

at that particular time set any GIF such that http://classicprocessing.en.ecplaza.net/mainl.gif

Regards

Phoenix XL

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

  • Moderators

tfabris,

the query takes a long time (5 seconds or more)

Is it really such a problem that your GUI becomes unresponsive for a mere 5 seconds? ;)

I would go with PhoenixXL - just disable the GUI and use a marquee progress to indicate to the user that something is going on. :)

If you really want to kep the GUI active then you might want to run the query in a separate script. You could compile it as a .a3x and FileInstall it - then you just run it using one of the methods shown in the Help file <Using AutoIt - Command Line Parameters - AutoIt specific command Line Switches>. As all you are returning is a single integer, you need not get too involved in inter-script communication to retrieve it. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...