Frayzer Posted October 15, 2008 Share Posted October 15, 2008 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) WEndEndFuncFunc _exit() ExitEndFuncWhile 1 Sleep(1000)WEndif 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 Link to comment Share on other sites More sharing options...
Skruge Posted October 15, 2008 Share Posted October 15, 2008 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 More sharing options...
Frayzer Posted October 16, 2008 Author Share Posted October 16, 2008 so i cant have a loop in a function? that's bad.. ok but thx Link to comment Share on other sites More sharing options...
dbzfanatic Posted October 16, 2008 Share Posted October 16, 2008 You can have a loop inside a function so long as you tell it to terminate so the rest of your code (the main logic) can execute. Go to my website. | My Zazzle Page (custom products)Al Bhed Translator | Direct linkScreenRec ProSimple Text Editor (STE) [TUTORIAL]Task Scheduler UDF <--- First ever UDF!_ControlPaste() UDF[quote name='renanzin' post='584064' date='Sep 26 2008, 07:00 AM']whats help ?[/quote] Link to comment Share on other sites More sharing options...
PsaltyDS Posted October 16, 2008 Share Posted October 16, 2008 so i cant have a loop in a function? that's bad..ok but thxNot 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. 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 More sharing options...
Frayzer Posted October 16, 2008 Author Share Posted October 16, 2008 ah okay, thanks for the description. i found a solution for my problem now: adlibenable it works now thanks to all Link to comment Share on other sites More sharing options...
rasim Posted October 16, 2008 Share Posted October 16, 2008 FrayzerAlso 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 EndFuncOr process the WM_COMMAND message Link to comment Share on other sites More sharing options...
Frayzer Posted October 16, 2008 Author Share Posted October 16, 2008 wow, thanks, that's really nice. i think ill use this. =) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now