goss34

Gui Freezes while function runs

13 posts in this topic

Hi Guys,

I am sure 99% of the members here will know the answer to this but I am having a problem figuring it out, iv searched the forums but cant find what i am after.

Essentially I am trying to write a little monitor that refreshes a gui every minute, currently the gui reads an ini and displays it on the gui as desired, it also updates the gui constantly but if i extend the sleep timer in my while loop then i cannot exit the gui ... i know this isnt the right way to do it but i am unsure how to do it correctly.

Heres my current code:

#include <GUIConstantsEx.au3>
    Global $iL = 10 , $iT = 10

DetectDate()
Func DetectDate()
    Global $Datestamp = @MDAY & "/" & @MON & "/" & @YEAR
EndFunc ;==>DetectDate

CreateGui()
Func CreateGui()
    Global  $GUI = GUICreate("BACS Monitor", 600, 600)
EndFunc ;==>CreateGui

Func ShowGui()
            Global $INI = ".\Test.ini"
            Global $aArray = IniReadSection($INI, $Datestamp)
            Local $DateLabel = GUICtrlCreateLabel($datestamp, 10,10)
            For $i = 1 To $aArray[0][0]
            Global $Status = IniRead($INI, $aArray[$i][0], "Status", "")
                If $Status = "Red" Then
                $Icon = "C:\Red.ico"
                ElseIf $Status = "Green" Then
                $Icon = "C:\Green.ico"
                EndIf
            GUICtrlCreateLabel($aArray[$i][0], $iL + 10 , $iT + 20)
            GUICtrlCreateIcon($Icon, -1, $iL + 100, $iT + 20,15,15)
            $iT = $iT + 20
            Next
            GUISetState(@SW_SHOW, $GUI)
            Global $iL = 10 , $iT = 10
EndFunc   ;==>ShowGui

    While 1
    ; Loop until the user exits.
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
            Case Else
                ShowGui()
        EndSwitch
    Sleep(50)
    WEnd

At the moment the ShowGui function just runs and runs which is fine but its too fast, how do i slow it down to run that function every 60 seconds without making the GUI hang?

By hang i mean the red cross (top right) is unresponsive which i assume is because the function is running so the GuiGetMsg is no longer monitoring for the click of the red cross.

Thanks

Dan 

Share this post


Link to post
Share on other sites



Take a look at timerinit and timerdiff functions and incorporate them into your for loop.


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

Hi Computergroove,

The help file for timerinit just appears to use a sleep, would that not just give me the same problem whereby while the sleep is active the gui would be unresponsive?

Ta

Dan

Share this post


Link to post
Share on other sites

Hi,

I think you need to use event mode Gui, not message mode. (you should not put Sleep in main loop with message mode anyway)

Share this post


Link to post
Share on other sites

Hi Teks,

Is that possible when i want the gui to constantly refresh every 60 seconds without the user clicking any buttons on the gui? From what i have just read i cant see an example of how to do that.

I only have the sleep in to stop the CPU maxing out.

Cheers

Dan

Share this post


Link to post
Share on other sites

goss34, yes possible, use event mode gui, and timerinit+timerdiff as computergroove suggested. I can post a simple example if you want

Share this post


Link to post
Share on other sites

Hi Teks,

If you could post an example that would be great.

Share this post


Link to post
Share on other sites

goss34,

Does this help?

#include <GUIConstantsEx.au3>

$hGUI = GUICreate("Test", 500, 500)

$cLabel = GUICtrlCreateLabel("0", 10, 10, 20, 20)

GUISetState()

; Get a timestamp
$nBegin = TimerInit()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    ; See if the timer delay has expired - here it is 5 secs

    If TimerDiff($nBegin) > 5 * 1000 Then
        ; Read the value displayed
        $sValue = GUICtrlRead($cLabel)
        ; Increase it by 1 and reset
        GUICtrlSetData($cLabel, $sValue + 1)
        ; Reset the timestamp for the next delay
        $nBegin = TimerInit()
    EndIf
WEnd

Teks,

Using Event mode will not help here - as you can see from the above you can very easily do this in either mode.

M23

1 person likes this

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

#9 ·  Posted (edited)

Melba23,

thanks for the example. What makes GUIGetMsg() return there since there's no user interaction? Am I missing something about how message mode works?

edit: wow, it immediately returns with 0 when there's no event. I didn't know that. That's effectively a busy loop but somehow does not hog the CPU. now I'm curious why event mode was invented.

Edited by Teks
add reason

Share this post


Link to post
Share on other sites

Teks,

In fact GUIGetMsg sleeps for about 12-15ms at every call. A clever little wrinkle from Jon which means you do not have to add a Sleep in the idle loop when in MessageLoop mode - but you certainly do need one if you are in OnEvent mode.

Both modes have their pros and cons.  I nearly always use MessageLoop mode as it means fewer functions to write for simple scripts - but there have been reports (from people I trust) that having a HUGE MessageLoop loop (with perhaps 100+ controls) can cause problems, although I have never come across it myself.

M23

1 person likes this

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

goss34,

Does this help?

#include <GUIConstantsEx.au3>

$hGUI = GUICreate("Test", 500, 500)

$cLabel = GUICtrlCreateLabel("0", 10, 10, 20, 20)

GUISetState()

; Get a timestamp
$nBegin = TimerInit()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    ; See if the timer delay has expired - here it is 5 secs

    If TimerDiff($nBegin) > 5 * 1000 Then
        ; Read the value displayed
        $sValue = GUICtrlRead($cLabel)
        ; Increase it by 1 and reset
        GUICtrlSetData($cLabel, $sValue + 1)
        ; Reset the timestamp for the next delay
        $nBegin = TimerInit()
    EndIf
WEnd

Teks,

Using Event mode will not help here - as you can see from the above you can very easily do this in either mode.

M23

It certainly does Melba, thank you very much, i have managed to get what i want going now and can continue with the script.

Cheers

Dan

Share this post


Link to post
Share on other sites

goss34,

Delighted I could help, but when you reply, please use the "Reply to this topic" button at the top of the thread or the "Reply to this topic" editor at the bottom rather than the "Quote" button - I know what I wrote and it just pads the thread unnecessarily.

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

Not a problem, noted. Thanks again

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