Jump to content

problem with button..


Frayzer
 Share

Recommended Posts

hey all,

i've got a problem with a button:

#include <GUIConstants.au3>

Opt("GUIOnEventMode", 1)

HotKeySet("{F9}", "_exit")

GUICreate("test", 100, 50)

$b1 = GUICtrlCreateButton("Button1", 10, 10)

GUISetState()

GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")

GUICtrlSetOnEvent($b1, "_run")

Func _run()

While 1

Sleep(4000)

WEnd

EndFunc

Func _exit()

Exit

EndFunc

While 1

Sleep(1000)

WEnd

if i press F9, i can close the gui but if i click on the button im not able to close it.

is there any solution for this problem? thanks :P

Link to comment
Share on other sites

Problem is, your _run function has a while loop, so no gui events are processed until it ends (never).

Depending on what you want the button to do, it should either hide/disable the GUI while processing, or return as quickly as possible and do any processing in the main loop.

#include <GUIConstants.au3>
Opt("GUIOnEventMode", 1)
HotKeySet("{F9}", "_exit")

Global $bActive = False
Global $iCount = 0

GUICreate("test", 100, 50)
$b1 = GUICtrlCreateButton("Start", 10, 10)
GUISetState()

GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")
GUICtrlSetOnEvent($b1, "_run")

While 1
    If $bActive Then
        $iCount += 1
        GUICtrlSetData($b1, $iCount)
    EndIf
    Sleep(1000)
WEnd

Func _run()
    $bActive = Not $bActive
    If Not $bActive Then
        GUICtrlSetData($b1, "Start")
        $iCount = 0
    EndIf
EndFunc   ;==>_run

Func _exit()
    Exit
EndFunc   ;==>_exit

btw, for legibility reasons you shouldn't have function declarations in the middle of your main logic.

[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

Link to comment
Share on other sites

Link to comment
Share on other sites

so i cant have a loop in a function?

that's bad..

ok but thx

Not exactly. You can have a loop in a function, and many functions have them.

The exact problem is that the function was called by a GUI event. Once an event's function is called, by clicking the button which triggers an event that calls _run() in your example, no other GUI (or GUI control) events can be processed until the first event's function returns. So until _run() returns, no further events are processed. It is important to keep your event handling functions short and sweet. Usually they just change some flags or do short, quick data manipulations and return, as in the example by Skruge.

Because your _run() function never returns, it blocks all further event processing.

:P

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

Frayzer

Also you can use this trick:

#include <GUIConstants.au3>

Opt("GUIOnEventMode", 1)
HotKeySet("{F9}", "_exit")

GUICreate("test", 100, 50)
GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")

$b1 = GUICtrlCreateButton("Button1", 10, 10)
GUICtrlSetOnEvent($b1, "_run")

GUISetState()

While 1
    Sleep(100)
WEnd

Func _run()
    Local $iPrevMode = Opt("GUIOnEventMode", 0)
    
    While 1
        If GUIGetMsg() = $GUI_EVENT_CLOSE Then
            Opt("GUIOnEventMode", $iPrevMode)
            _exit()
        EndIf
        Sleep(100)
        ConsoleWrite("_run" & @LF)
    WEnd
    
    Opt("GUIOnEventMode", $iPrevMode)
EndFunc

Func _exit()
    Exit
EndFunc

Or process the WM_COMMAND message :P

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