Jump to content

OnEvent in GUI


Rhyono
 Share

Recommended Posts

My program spends most of its time in a function that is being looped. I am using GUISetOnEvent for exiting the program, but it appears that it only checks on that when not in the loop. Once the loop starts however, it doesn't end until it meets a user specified time, which could be hours away. What can I do so that the X button always works?

Link to comment
Share on other sites

Once the loop starts however, it doesn't end until it meets a user specified time, which could be hours away

Make the loop to be of maximum 20millisecs orelse dont use events for your GUI use msg instead, inside the loop

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

Rhyono,

This works for me

#include <GUIConstants.au3>

Opt('GUIOnEventMode', 1)    

local $gui010 = guicreate('')
GUISetOnEvent($gui_event_close,'_exit',$gui010)
GUISetState(@SW_SHOW)

while 1
wend

func _exit()
    Exit
endfunc

I would say that the loop is pretty tight. Is the guisetonevent after the guicreate stmt? Anything further you'll have to do as JLogan3o13 suggests.

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

It's easier to just explain the code than post the whole thing:

GUISetOnEvent for the close and start is here

The loop is here

Then the start button initiates a function that is not the loop down here, but loops every three seconds.

@kylomas That works just fine, UNTIL my looping function begins. Since it never leaves the function after that (except to go to other functions), it never returns control back to the GUI in order for the event to be noticed.

Link to comment
Share on other sites

I assumed it's because my function never truly exits itself. The X button works up until I start the self-looping portion of the program. Once I'm in that loop, it never gives it a chance to check.

Here's a simplified version, but with the same results:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1) ; Change to OnEvent mode
$Main = GUICreate("Title", 317, 207, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE,"quit") ; Enable closing
$startButton = GUICtrlCreateButton("Start", 81, 160, 75, 25)
GUICtrlSetOnEvent($startButton,"begin")
GUISetState(@SW_SHOW)

While 1
Sleep(1000)
WEnd

Func begin()
; Other stuff is done here in the main program, it isn't just a pointless redirect to another function
main_func()
EndFunc

Func main_func()
Sleep(3000)
main_func()
EndFunc


Func quit()
Exit
EndFunc
Edited by Rhyono
Link to comment
Share on other sites

Ryhono,

I was hoping that it was the recursion in main_func(), however, this code has no recursion and exhibits the exact same behavior.

#include <GUIConstants.au3>

Opt('GUIOnEventMode', 1)

local $gui010 = guicreate('')
GUISetOnEvent($gui_event_close,'_exit',$gui010)
local $btn010   =   guictrlcreatebutton('start',0,0,100,20)
guictrlsetonevent(-1,'_start')
GUISetState(@SW_SHOW)

while 1
wend

func _start()

    while 1
        sleep(100000)
    wend

endfunc

func _exit()
    Exit
endfunc

Maybe a gui heavy hitter will weigh in on this.

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Ryhono,

Interestingly, when the func is not driven by an event it works.

#include <GUIConstants.au3>

Opt('GUIOnEventMode', 1)

local $gui010 = guicreate('')
GUISetOnEvent($gui_event_close,'_exit',$gui010)
local $btn010   =   guictrlcreatebutton('start',0,0,100,20)
guictrlsetonevent(-1,'_start')
GUISetState(@SW_SHOW)

;~ while 1
;~ wend

_start()

func _start()

    while 1
        sleep(100000)
    wend

endfunc

func _exit()
    Exit
endfunc

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Autoit is Single-Threaded thats maybe the answer !! :huh:

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

  • Moderators

kylomas,

Reading the Interrupting a running function tutorial in the Wiki might help. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

M23,

Thanks, it was this simple!

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1) ; Change to OnEvent mode
$Main = GUICreate("Title", 317, 207, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE,"quit") ; Enable closing
$startButton = GUICtrlCreateButton("Start", 81, 160, 75, 25)
GUICtrlSetOnEvent($startButton,"begin")
GUISetState(@SW_SHOW)

guiregistermsg($wm_command,'_return')

While 1
Sleep(1000)
WEnd

Func begin()
; Other stuff is done here in the main program, it isn't just a pointless redirect to another function
main_func()
EndFunc

Func main_func()
Sleep(3000)
main_func()
EndFunc

func _return($hWnd, $Msg, $wParam, $lParam)
    ConsoleWrite(bitand($wparam,0x0000ffff) & @LF)
    if bitand($wparam,0x0000ffff) = $wm_destroy then quit()
endfunc


Func quit()
Exit
EndFunc

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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