Jump to content

GUI Loop


 Share

Recommended Posts

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _Switch()
        Case $Button2
            _ClearConfig()
        Case $Button3
            _ByPass()
    EndSwitch

    If $temp = 0 Then
        _ServerStatus()
        $temp = $temp + 1
    EndIf

    _ServerUpdate()
    Sleep(1000)
WEnd

My buttons wont work if I set Sleep command, If i dont, then program will be lagging as hell

_ServerUpdate() ve InetRead etc. inside

Link to comment
Share on other sites

Your buttons is just fine, it's just that you won't see them when you Sleep() for a whole second when there's dozens (if not more) of items on the queue.

If your app really is heavy enough to warrant a Sleep(), then use a more sensible value like 10 ms.

Or move the heavy stuff out of the main-loop.

Link to comment
Share on other sites

If your app really is heavy enough to warrant a Sleep(), then use a more sensible value like 10 ms.

This is cause of freezes... i cant write fluently

and i need to put all stuff into loop because I am updating 2 lables colors (its checking server status - Online is green, Offline is blue, I am using InetRead for this)

If Server offline Then PaintRed

ofc I added more ifs and temp vars to avoid label-blinks

Link to comment
Share on other sites

I'm sure you're spending all your time in the _ServerStatus and/or _ServerUpdate functions, as you call them each time through your GUIGetMsg() loop. You need to throttle back the frequency of calls to the server routines to allow some processing time to be spent in your main loop. One method, of many, might be to put your server functions on a timer. There's not enough of your example code to see exactly what you're trying to do, but this might provide you with something useful:

#include <Timers.au3>
#include <GUIConstantsEx.au3>


Global $ScanStatus, $ServerTimer
$GUI = GUICreate("", 200, 120)
$Button_Scan = GUICtrlCreateButton("START SCAN", 50, 20, 100, 22)
$Button1 = GUICtrlCreateButton("Button1", 15, 60, 50, 22)
$Button2 = GUICtrlCreateButton("Button2", 75, 60, 50, 22)
$Button3 = GUICtrlCreateButton("Button3", 135, 60, 50, 22)
$Label1 = GUICtrlCreateLabel("", 45, 95, 120, 22)
GUICtrlSetColor(-1, 0xFF0000)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case -3
            Exit
        Case $Button_Scan
            If $ScanStatus Then ; stop timer
                _Timer_KillTimer($GUI, $ServerTimer)
                GUICtrlSetData($Button_Scan, "START SCAN")
            Else ; start timer
                $ServerTimer = _Timer_SetTimer($GUI, 3000, "_CheckServer", -1) ; call _CheckServer() every 3 seconds
                GUICtrlSetData($Button_Scan, "STOP SCAN")
            EndIf
            $ScanStatus = Not $ScanStatus ; toggle flag
        Case $Button1
            Beep(784, 50)
        Case $Button2
            Beep(880, 50)
        Case $Button3
            Beep(988, 50)
    EndSwitch
WEnd

Func _CheckServer($hWnd, $iMsg, $iwParam, $ilParam)
    GUICtrlSetState($Button1, $GUI_DISABLE)
    GUICtrlSetState($Button2, $GUI_DISABLE)
    GUICtrlSetState($Button3, $GUI_DISABLE)
    GUICtrlSetData($Label1, "...POLLING SERVER...")
    Sleep(500)
    GUICtrlSetData($Label1, "")
    GUICtrlSetState($Button1, $GUI_ENABLE)
    GUICtrlSetState($Button2, $GUI_ENABLE)
    GUICtrlSetState($Button3, $GUI_ENABLE)
;    _ServerStatus()
;    _ServerUpdate()
EndFunc

PS - Nothing will (or can) happen in your GUIGetMsg() loop while any of your called server functions are executing. Program execution can't be in two places at one time. So you would want to tune the server routines for speed, and adjust your timer to allow enough "free time" in the main loop to give the buttons a semblance of responsiveness.

Edited by Spiff59
Link to comment
Share on other sites

When you wrote "timer" how stiupid I was =]

new code

$temp = 0
$timer = TimerInit()
$t_verf = 1500
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _Switch()
        Case $Button2
            _ClearConfig()
        Case $Button3
            _ByPass()
    EndSwitch

    If $temp = 0 Then
        _ServerStatus()
        $temp = $temp + 1
    EndIf
    $t_diff = TimerDiff($timer)

    If $t_diff / 2 > $t_verf Then
        _ServerUpdate()
        ConsoleWrite($t_diff & @CRLF)
        $t_verf = $t_verf + 1500
    EndIf
WEnd

Thx

Edited by Hell4Ge
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...