Jump to content

Have to click exit twice to leave script


Recommended Posts

I am working on an AutoIt script using OnEvent mode. For some reason, I have to click on the Exit button twice to successfuly leave the script? I just can't see what I am doing wrong.

; AutoIt Pickout Script
; 
#include <GuiConstantsEx.au3>
#include <AVIConstants.au3>
#include <TreeViewConstants.au3>
#include <Debug.au3>

Opt('MustDeclareVars', 1)
_DebugSetup ("Pickout Monitoring", True)   ; start displaying debug 
_DebugOut ("Starting")  

_Main()
MsgBox(0, "Exiting", "Exiting Pickout Monitoring...")
_DebugOut ("Ending")  


Func _Main()

Local $msg
Local $cancelbutton
Local $mainwindow
Local $exitbutton
Local $startscanningbutton
Local $sampleID

Opt("GUIOnEventMode", 1)  ; Change to OnEvent mode 

$mainwindow = GuiCreate("Pickout Detection", 350, 150)
GuiSetIcon(@SystemDir & "\mspaint.exe", 0)

GuiCreate("Pickout Detection", 350, 150)

; INPUT
GuiCtrlCreateLabel("Sample ID", 60, 40)
$sampleID = GuiCtrlCreateInput("Sample ID", 120, 40, 130, 20)

; BUTTON
$startscanningbutton = GuiCtrlCreateButton("Start Scanning Process", 30, 80, 150, 30)
GUICtrlSetOnEvent($startscanningbutton, "StartScanning")

$exitButton =GuiCtrlCreateButton("Exit", 210, 80, 100, 30)
GUICtrlSetOnEvent($exitButton, "ExitClicked")

GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")

GUISetState(@SW_SHOW)

While 1
  Sleep(100)  ; Idle around
WEnd

;   MoveFolder("C:\temp", "C:\temp5", "Sample1")

Exit
EndFunc   ;==>_Main

Func ExitClicked()
  ;Note: at this point @GUI_CTRLID would equal $GUI_EVENT_CLOSE, 
  ;and @GUI_WINHANDLE would equal $mainwindow 
    MsgBox(0, "GUI Event", "You clicked ExitClick...")
  Exit
EndFunc

Func StartScanning()
  ;Note: at this point @GUI_CTRLID would equal $GUI_EVENT_CLOSE, 
  ;and @GUI_WINHANDLE would equal $mainwindow   
  MsgBox(0, "GUI Event", "You clicked StartScanning...")
EndFunc

func CreateFolder($RootFolder, $FolderName)
    Local $err
    $err = DirCreate($RootFolder & "\" & $FolderName)
    _DebugOut ("Error: " & $err)
EndFunc

func MoveFolder($Source, $Destination, $SampleName)
    Local $err
    $err = DirMove($Source, $Destination & "\" & $SampleName)
        _DebugOut ($Source)
        _DebugOut ($Destination & "\" & $SampleName)
        _DebugOut ("Error: " & $err)
    $err = DirCreate($Source)
        _DebugOut ("Error: " & $err)
EndFunc

Func SpecialEvents()
    Select
        Case @GUI_CtrlId = $GUI_EVENT_CLOSE
            MsgBox(0, "Close Pressed", "ID=" & @GUI_CtrlId & " WinHandle=" & @GUI_WinHandle)
            Exit
    EndSelect
EndFunc   ;==>SpecialEvents
Edited by ijourneaux
Link to comment
Share on other sites

It has something to do with _DebugSetup containing OnAutoItExitRegister("__Debug_ReportClose").

If you add OnAutoItExitUnRegister("__Debug_ReportClose") before the Exit it exits as expected, but I don't know if that's a good idea.

Edit: I think the script gets stuck in __Debug_ReportWindowWaitClose() (called by __Debug_ReportClose() which is registered to run on exit)

Edit2: Line 510 in debug.au3:

Local Const $GUI_EVENT_CLOSE = -3
    While WinExists(HWnd($hWndReportWindow))
        If GUIGetMsg() = $GUI_EVENT_CLOSE Then GUIDelete($hWndReportWindow)
    WEnd

That won't do much in OnEventMode.

Edited by Tvern
Link to comment
Share on other sites

Uhm... how many GUIs did you mean to have?

Opt("GUIOnEventMode", 1)  ; Change to OnEvent mode 

$mainwindow = GuiCreate("Pickout Detection", 350, 150)
GuiSetIcon(@SystemDir & "\mspaint.exe", 0)

GuiCreate("Pickout Detection", 350, 150)
:mellow:
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Turning DebugMode off make the script work as expected. That alows me to move forward. Is this expected behavior? Perhaps DebugMode should not be used in EventMode?

Thanks for the help.

It has something to do with _DebugSetup containing OnAutoItExitRegister("__Debug_ReportClose").

If you add OnAutoItExitUnRegister("__Debug_ReportClose") before the Exit it exits as expected, but I don't know if that's a good idea.

Edit: I think the script gets stuck in __Debug_ReportWindowWaitClose() (called by __Debug_ReportClose() which is registered to run on exit)

Link to comment
Share on other sites

Good catch of one of my errors as I learn AutoIt. This doesn't seem to explain the problem with having to click on exit twice. Turning Debug off fixes the problem.

Uhm... how many GUIs did you mean to have?

:mellow:

Edited by ijourneaux
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...