Jump to content

Events handling : event is not detected


Recommended Posts

Hi all,

I've been trying to find why the cript below doesn't work for some time... It's probably obvious but it puzzles me.

#include-once


#include <GUIConstantsEx.au3>


global $exitCountdown = false
global $windows[256]
global $windowOnTop = 1


Func MakeModal($windowHandle)
    $windows[$windowOnTop] = $windowHandle

    if $windowOnTop>1 then ; do that for all windows except the main window (i.e. the first one)
        GUISetState(@SW_DISABLE, $windows[$windowOnTop-1]) ; disable current
        GUISetOnEvent($GUI_EVENT_CLOSE, 'CloseModal', $windowHandle)
        GUISwitch($windowHandle)    ; switch to the new modal window        
        GUISetState(@SW_SHOW, $windowHandle)
    EndIf
    
    If $windowOnTop < 255 then 
        $windowOnTop = $windowOnTop + 1
    Else
        MsgBox(0, 'Error', 'Too many modal windows')
    EndIf
EndFunc

Func CloseModal()
    if $windowOnTop > 2 then ; we need at least 2 windows (the main one and a modal)
        $windowOnTop  = $windowOnTop-1

        GUISetState(@SW_HIDE, $windows[$windowOnTop]) ; hide the modal
        GUIDelete($windows[$windowOnTop]) ; destroy the modal
        $windows[$windowOnTop] = -1 ;security
        GUISetState(@SW_ENABLE, $windows[$windowOnTop-1]) ; enable window below
        
        GUISwitch($windows[$windowOnTop-1]) ; switch to the previous
        GUISetState(@SW_SHOW, $windows[$windowOnTop-1])
        
        WinActivate($windows[$windowOnTop-1])
    EndIf
EndFunc
        
    
Func GUI_OnGiveControl()
    ConsoleWrite("Click"&@CRLF)
    $exitCountdown = true
EndFunc



func CreatePopupWindow()
    $frmNoControl = GUICreate(" ", 283, 100, 450, 219)
    $btnGiveControl = GUICtrlCreateButton("", 8, 10, 265, 33, 0)
    GUISetState(@SW_SHOW)   
    MakeModal($frmNoControl)
    
    GUISwitch($frmNoControl)
    GUICtrlSetOnEvent($btnGiveControl, 'GUI_OnGiveControl')
    
    GUISetState(@SW_SHOW)

    $countDown = 15 ;15 secs
    $exitCountdown = false
    while not $exitCountdown
        GUICtrlSetData($btnGiveControl, "Discarding this window in ... "&$countDown&"s")

        if $countDown > 0 then 
            $countDown -= 1
        Else
            $exitCountdown = True
        EndIf

        GUISwitch($frmNoControl)
        ConsoleWrite("Discard window is active"&@CRLF)
        
        Sleep (1000)

    WEnd

    CloseModal()
EndFunc




Func Stalk_CloseApplication()
    Exit
EndFunc

Func Stalk_OnOK()
    CreatePopupWindow()
EndFunc

Func CreateMainWindow()
    $title = "Test Stalk"
    $frmTest = GUICreate($title,150, 150, 600, 400)
    ConsoleWrite("$frmTest: "&$frmTest&@CRLF)
    GUISwitch($frmTest)
    $btnStalk_OK = GUICtrlCreateButton("Go", 0, 0, 150, 150, 0)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    MakeModal($frmTest)
    GUISetState(@SW_SHOW)

    GUISetOnEvent($GUI_EVENT_CLOSE, 'Stalk_CloseApplication')
    GUICtrlSetOnEvent($btnStalk_OK, 'Stalk_OnOK')
    ConsoleWrite("Main created."&@CRLF)

EndFunc


Func MainFunc()
    
    Opt("GUIOnEventMode", 1)
    Opt("TrayIconDebug", 1)

    ConsoleWrite("Start."&@CRLF)

    CreateMainWindow()

    While 1
        Sleep(1000)
        ConsoleWrite("Main3"&@CRLF)
    WEnd

EndFunc


MainFunc()

As you can see if you run the script, the popup window won't close if you click on the button.

The event seems not to be detected.

I suspect it's because the wrong GUI is active at that time, but I can't solve the issue.

Can someone help me?

Link to comment
Share on other sites

Hi..

Your while loop in CreatePopupWindow() function is executed continously, thats why the GUI events aren't registered. As autoit doesn't support multithreading, it can't do two things simultaneously.

[font="Garamond"]Manjish Naik[/font]Engineer, Global Services - QPSHoneywell Automation India LimitedE-mail - Manjish.Naik@honeywell.com
Link to comment
Share on other sites

Simultaneously to what? It kinda replaces the main loop and shouldn't prevent the event to be fired...

What would be the best way to proceed, then?

Oh I think I got it.

- Every time an event is fired, then it's queued.

- An event cannot be dequeued until the whole script fired by the previous event has been executed.

- When I click on "Go", it triggers an event, and, because of the way my code is written, the popup window's loop is part of that "event" code.

- Therefore, any action performed while I'm still displaying that window *cannot* be unqueued.

I'll try to change the code to fix that.

Link to comment
Share on other sites

See once the script goes into the while loop.. it will continue counting down till your 15 secs are up and $exitCountdown is set to true.. Till this time it won't do anything else..

I would change things around here.. But I still am not exactly sure what u wanna do.. So pls explain in simple terms what u want the script to do..

[font="Garamond"]Manjish Naik[/font]Engineer, Global Services - QPSHoneywell Automation India LimitedE-mail - Manjish.Naik@honeywell.com
Link to comment
Share on other sites

I'll try to change the code to fix that.

the modified script below solves the issue

#include-once


#include <GUIConstantsEx.au3>


;~ global $exitCountdown = false
global $startCountdown = False
global $countDown = 0
global $btnGiveControl = -1

global $windows[256]
global $windowOnTop = 1


Func MakeModal($windowHandle)
    $windows[$windowOnTop] = $windowHandle

    if $windowOnTop>1 then ; do that for all windows except the main window (i.e. the first one)
        GUISetState(@SW_DISABLE, $windows[$windowOnTop-1]) ; disable current
        GUISetOnEvent($GUI_EVENT_CLOSE, 'CloseModal', $windowHandle)
        GUISwitch($windowHandle)    ; switch to the new modal window        
        GUISetState(@SW_SHOW, $windowHandle)
    EndIf
    
    If $windowOnTop < 255 then 
        $windowOnTop = $windowOnTop + 1
    Else
        MsgBox(0, 'Error', 'Too many modal windows')
    EndIf
EndFunc

Func CloseModal()
    if $windowOnTop > 2 then ; we need at least 2 windows (the main one and a modal)
        $windowOnTop  = $windowOnTop-1

        GUISetState(@SW_HIDE, $windows[$windowOnTop]) ; hide the modal
        GUIDelete($windows[$windowOnTop]) ; destroy the modal
        $windows[$windowOnTop] = -1 ;security
        GUISetState(@SW_ENABLE, $windows[$windowOnTop-1]) ; enable window below
        
        GUISwitch($windows[$windowOnTop-1]) ; switch to the previous
        GUISetState(@SW_SHOW, $windows[$windowOnTop-1])
        
        WinActivate($windows[$windowOnTop-1])
    EndIf
EndFunc
        
    
Func GUI_OnGiveControl()
    ConsoleWrite("Click"&@CRLF)
    $countDown = 0  ; finish countDown
EndFunc



func CreatePopupWindow()
    $frmNoControl = GUICreate(" ", 283, 100, 450, 219)
    $btnGiveControl = GUICtrlCreateButton("", 8, 10, 265, 33, 0)
    GUISetState(@SW_SHOW)   
    
    MakeModal($frmNoControl)
    
    GUISwitch($frmNoControl)
    GUICtrlSetOnEvent($btnGiveControl, 'GUI_OnGiveControl')
    
    GUISetState(@SW_SHOW)


EndFunc




Func Stalk_CloseApplication()
    Exit
EndFunc

Func Stalk_OnOK()
    $startCountDown = true
EndFunc

Func CreateMainWindow()
    $title = "Test Stalk"
    $frmTest = GUICreate($title,150, 150, 600, 400)
    ConsoleWrite("$frmTest: "&$frmTest&@CRLF)
    GUISwitch($frmTest)
    $btnStalk_OK = GUICtrlCreateButton("Go", 0, 0, 150, 150, 0)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    MakeModal($frmTest)
    GUISetState(@SW_SHOW)

    GUISetOnEvent($GUI_EVENT_CLOSE, 'Stalk_CloseApplication')
    GUICtrlSetOnEvent($btnStalk_OK, 'Stalk_OnOK')
    ConsoleWrite("Main created."&@CRLF)

EndFunc

Func HandleCountDown()
        if ($startCountDown) Then
            CreatePopupWindow()
            $countDown = 15 ;15 secs
            $startCountDown = false
        EndIf
    
        if $countDown > 0 then 
            ConsoleWrite("Discard window is active"&@CRLF)
            GUICtrlSetData($btnGiveControl, "Discarding this window in ... "&$countDown&"s")
            $countDown -= 1
            return true ;the countdown is active
        Elseif $countDown = 0 then 
            CloseModal()
            $countDown -= 1
            return true ;the countdown is active
        EndIf
        
        return false ;the countdown is not active
        
EndFunc

Func MainFunc()
    
    Opt("GUIOnEventMode", 1)
    Opt("TrayIconDebug", 1)

    ConsoleWrite("Start."&@CRLF)

    CreateMainWindow()

    
    While 1
        
        if not HandleCountDown() then 
            ; the popup is closed
            ConsoleWrite("Main3"&@CRLF)
        EndIf
        
        Sleep(1000)
    WEnd

EndFunc


MainFunc()
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...