Jump to content

Trying to Close GUI during FOR loop


Recommended Posts

I have a GUI app that I am working on.  I looked at GUIRegisterMsg and its just not making sense.  As I understand it, I am supposed to use GUIRegisterMsg to capture the GUI_Event_Close message that calls a function to set a flag.  Does anyone have a simple example that they use?

Link to comment
Share on other sites

GUIRegisterMsg is used to capture Windows messages that are not captured by the AutoIt message loop. There is rarely a need to capture messages that have already been captured by the AutoIt message loop. Please explain what it is you want to do.

Link to comment
Share on other sites

Here are two ways.

HotKeySet("+{Esc}", "_Exit")

GUICreate("GUI")
GUISetState()

While 3
    Switch GUIGetMsg()
        Case -3
            Exit
    EndSwitch
    _CodeLoop()
WEnd

Func _CodeLoop()
    While 3
        Sleep(50)
        If GUIGetMsg() = -3 Then Exit
    WEnd
EndFunc

Func _Exit()
    Exit
EndFunc

 

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

There are 2 other ways  :)

Both can be used, GuiOnEventMode is simpler to use but GUIRegisterMsg has a higher priority as shown in the tests below (can be useful in some rare cases)

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
HotKeySet("{ESC}", "_Terminate2")

Opt("GuiOnEventMode", 1)

GUICreate("My GUI") 
GUISetOnEvent($GUI_EVENT_CLOSE, "_Terminate")
$btn = GUICtrlCreateButton("stop", 10, 50, 50, 20)
GUICtrlSetOnEvent(-1, "_btn")
$label = GUICtrlCreateLabel("0", 100, 50, 20, 20)
GUISetState() 
 
GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")
GUIRegisterMsg($WM_SYSCOMMAND, "_WM_SYSCOMMAND")  

Local $aAccelKeys[1][2] = [["{SPACE}", $btn]]
GUISetAccelerators($aAccelKeys)

For $i = 1 to 10000
   Sleep(300)
   GuiCtrlSetData($label, GuiCtrlRead($label)+1)
Next

;While 1
 ; Sleep(10)
;Wend

Func _btn()
        Msgbox(0,"", "on event")
EndFunc

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
 #forceref $hWnd, $Msg, $lParam
 Switch BitAND($wParam, 0xFFFF)
    Case $btn
        Msgbox(0,"", "wm_command")
 EndSwitch
  Return 'GUI_RUNDEFMSG'
EndFunc  

Func _WM_SYSCOMMAND($hWnd, $Msg, $wParam, $lParam)
 ; 0xF060 = $SC_CLOSE
    If BitAND($wParam, 0xFFF0) = 0xF060 Then Exit Msgbox(0,"", "wm_syscommand") 
EndFunc

Func _Terminate()
    Exit Msgbox(0,"", "on event")
EndFunc

Func _Terminate2()
    Exit Msgbox(0,"", "hotkey")
EndFunc

Should read this https://www.autoitscript.com/wiki/Interrupting_a_running_function

Edited by mikell
Link to comment
Share on other sites

I suspect my issue has something to do with being inside of a function. Because I do not believe I saw this issue until I made it a function.  My script is using OneventMode.  When I am inside the function, and inside the FOR loop, my GUI does not respond to a CLOSE until the loop finishes

 

Heres my function

Func Query()

        GUICtrlSetData($hList, "")

        $Pserver = GUICtrlRead ($hcombo)

      $objWMIService = ObjGet("winmgmts:\\" & $pserver & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_Printer")

For $objItem In $colItems


   _ArrayAdd ($PrinterInfo,$objitem.Name,0)
   GUICtrlSetData ($hList,$objitem.Name & ",")

      If StringLeft ($objItem.PortName, 3) = "IP_" Then
      $PrinterInfo[$x][1]=StringTrimLeft ($objItem.PortName, 3)
      Else
    $PrinterInfo[$x][1]=$objItem.PortName
    EndIf

   $State = $objItem.PrinterState

   if $State = 0 Then $State = "Ready"
   if $State = 1024 Then $State = "Ready"; 1024 is "Printing"
   if $State = 16384 Then $State = "Ready"; 16284 is "Processing"
   if $State = 1 Then $State = "Paused"
   if $State = 129 Then $State = "Paused - Offline"
   if $State = 1027 Then $State = "Paused - Error"
   If $State = 131073 Then $State = "Paused - Toner/Ink Low"
   if $State = 128 Then $State = "Offline"
   if $State = 1154 Then $State = "Error - Offline"
   if $State = 8 Then $State = "Paper Jam"
   if $State = 1034 Then $State = "Error - Paper Jam"
   if $State = 131072 Then $State = "Toner/Ink Low"
   If $State = 262144 Then $State = "No Toner/Ink"
   if $State = 2 Then $State = "Error"
   if $State = 132 Then $State = "Deleting - Error"
   if $State = 1158 Then $State = "Error - Deleting - Offline"
   if $State = 1042 Then $State = "Error - Out of Paper"
   if $State = 16 Then $State = "Out of Paper"
   If $State = 4194304 Then $State = "Door Open"

      $PrinterInfo[$x][2]=$state
      $PrinterInfo[$x][3]=$objItem.Drivername
       $PrinterInfo[$x][4]=$objItem.Location
        $PrinterInfo[$x][5]=$objItem.comment
      ;_ArrayDisplay($Printerinfo)
      $x = $x + 1
   Next
    _ArrayDisplay($Printerinfo)
    EndFunc

 

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...