Sign in to follow this  
Followers 0
Rhyono

OnEvent in GUI

22 posts in this topic

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?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hi,

Yes, just reset the onevent mode before entering in your loop and use the GUIGetMsg function.

...
Opt("GUIOnEventMode", 0)

While 1
$iMsg = GUIGetMsg()
If $iMsg = $GUI_EVENT_CLOSE Then ...
...
WEnd

Opt("GUIOnEventMode", 1)
...

Br, FireFox.

Edited by FireFox

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

Doing that results in my "start" button that uses GUISetOnEvent to not work.

Share this post


Link to post
Share on other sites

So how about posting your code so we're not left to guess? :)


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Rhyono,

To get any REAL help you will need to post the code or a reproducer.

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

Share this post


Link to post
Share on other sites

I sort of solved it by using a check box to determine if it is time to quit by having the looping function check the value of the check box each time.

Share this post


Link to post
Share on other sites

Rhyono,

Curious how/why your function is blocking the script?

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

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

It is.

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

FireFox,

Please explain.

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

Share this post


Link to post
Share on other sites

kylomas,

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

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

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

This is a great example.

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

Hmmm...that exact code doesn't work for me still, kylomas. Once the start button has been pressed, you can no longer exit the program still. :think:

Edited by Rhyono

Share this post


Link to post
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
Sign in to follow this  
Followers 0