Jump to content

Gui Freezes while function runs


 Share

Recommended Posts

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 

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

Link to comment
Share on other sites

  • Moderators

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

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

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
Link to comment
Share on other sites

  • Moderators

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

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

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

Link to comment
Share on other sites

  • Moderators

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

 

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

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