Jump to content

GUI's responsiveness


Recommended Posts

I realize that there is a forum just for help with GUIs, but I visit there so seldom, I would rather post this here.

I have this code working without a GUI. I use a regular input box and a second script to keep killing processes until the correct password is entered. I decided to see if a GUI could simplify things by having only one script. However, the GUI is unresponsive during the load of killing 15+ processes. I can live without the logging function (which removes the ProcessWaitClose and most of the loop's lag) then the GUI is fairly responsive - but I thought that I would ask for your input before doing that.

...and then there is the OnEvent mode that I've not dug into yet - would that be better suited for this task?

Thanks for you time.

AutoItSetOption("TrayIconDebug", 1) ;0-off
#include <GUIConstantsEx.au3>

Global $time = 1 ;norm is 179 minutes
$psw = "test"
$list = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15"
$listArray = StringSplit($list, ",")

GUICreate("test", 185, 125, -1, -1)
$Label1 = GUICtrlCreateLabel("New password.", 10, 10, -1, 17)
$Input1 = GUICtrlCreateInput("", 10, 30, 165, 20, 32) ;32 = $ES_PASSWORD
GUICtrlSendMsg($Input1, 0xCC, Asc("*"), 0)
GUICtrlSetState($Input1, @SW_UNLOCK)
$Button1 = GUICtrlCreateButton("OK", 10, 60, 75, 25, 0x0001) ;0x0001 = $BS_DEFPUSHBUTTON
$Button2 = GUICtrlCreateButton("Cancel", 100, 60, 75, 25)
$Button3 = GUICtrlCreateButton("Help", 10, 90, 165, 25)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg

        Case $GUI_EVENT_CLOSE
            GUISetState(@SW_HIDE)

        Case $Button1
            $Ans = GUICtrlRead($Input1)
            GUICtrlSetData($Input1, "")
            GUICtrlSetState($Input1, $GUI_FOCUS)

            If $Ans = "close-test" Then Exit

            If $Ans = $psw Then
                GUISetState(@SW_HIDE)
                Run("notepad.exe")
                CountDown()
            EndIf

        Case $Button2
            GUISetState(@SW_HIDE)

        Case $Button3
            SplashTextOn("test-help", "Send an e-mail to...." & _
                    @CR & @CR & "Click on this screen to exit.", -1, 100, Default, Default, 18)
            GUICtrlSetState($Input1, $GUI_FOCUS)
            ;see "end splash screen"

    EndSwitch

    ;end splash screen
    If WinActive("test-help", "Send an e-mail") Then SplashOff()

    If ProcessExists("notepad.exe") Then
        ProcessClose("notepad.exe")
        GUISetState(@SW_SHOW)
        GUICtrlSetState($Input1, $GUI_FOCUS)
        ProcessWaitClose("notepad.exe")
    EndIf

    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ; this is a 15+ second load that
    ; kills the GUI's responsiveness
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    For $i = 1 To $listArray[0]
        If ProcessExists($listArray[$i]) Then
            ProcessClose($listArray[$i])
            ;logging function goes here
            ProcessWaitClose($listArray[$i], 1)
            ;ProcessWaitClose is to prevent dozens
            ;of log entries while processes close
        EndIf
        Sleep(9)
    Next
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
WEnd


Func CountDown()
    For $i = ($time * 60) To 1 Step -1
        $h = ($i / 3600)
        $m = ($h - Int($h)) * 60
        $s = ($m - Int($m)) * 60
        ToolTip(Int($h) & "h " & Int($m) & "m " & Int($s) & "s", 0, 0)
        If Not ProcessExists("notepad.exe") Then ExitLoop
        Sleep(1000)
    Next
    ToolTip("")
    If ProcessExists("notepad.exe") Then
        ProcessClose("notepad.exe")
        ProcessWaitClose("notepad.exe")
    EndIf
EndFunc   ;==>CountDown

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

Why is it that notepad must be open before the script continues? I tested and it won't show until notepad is found and closed. I also commented out your ProcessWaitClose() lines and it seemed to work perfectly.

Link to comment
Share on other sites

Why is it that notepad must be open before the script continues? I tested and it won't show until notepad is found and closed. I also commented out your ProcessWaitClose() lines and it seemed to work perfectly.

Notepad must be open before the GUI shows. But the script should be running and killing the list of 15+ processes all the time. If the user attempts to use "notepad" he/she will be prompted for a password. Notepad is for testing/posting this sample code only.

On a weak computer under a bit of a load, the GUI is still pretty bad even without the ProcessWaitClose() line in the main loop. I may just go back to using a second script to kill the processes "full time" and let the GUI control that script.

Thanks for looking.

Any thoughts on the "OnEvent" method of coding a GUI?

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

OnEventMode is pretty good for advanced things but if you just need it to close if the password is wrong why not just do something like

While 1
If ProccessExists("notepad.exe") Then
If GUICtrlRead($input1) <> $password Then
ProccessClose("notepad.exe");not sure if you want to close the script or the exe
Exit;in case you want to close the script
Endif
Endif
Link to comment
Share on other sites

@herewasplato

try a combination of OnEventMode and a timer

Edit: forgot kill and restart timer

Opt("TrayIconDebug", 1) ;0-off
Opt("GUIOnEventMode", 1)

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

Global $time = 1 ;norm is 179 minutes
$psw = "test"
$list = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15"
$listArray = StringSplit($list, ",")

$hGUI = GUICreate("test", 185, 125, -1, -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Events")

$Label1 = GUICtrlCreateLabel("New password.", 10, 10, -1, 17)
$Input1 = GUICtrlCreateInput("", 10, 30, 165, 20, 32) ;32 = $ES_PASSWORD
GUICtrlSendMsg($Input1, 0xCC, Asc("*"), 0)
GUICtrlSetState($Input1, @SW_UNLOCK)

$Button1 = GUICtrlCreateButton("OK", 10, 60, 75, 25, 0x0001) ;0x0001 = $BS_DEFPUSHBUTTON
GUICtrlSetOnEvent(-1, "_Buttons")
$Button2 = GUICtrlCreateButton("Cancel", 100, 60, 75, 25)
GUICtrlSetOnEvent(-1, "_Buttons")
$Button3 = GUICtrlCreateButton("Help", 10, 90, 165, 25)
GUICtrlSetOnEvent(-1, "_Buttons")

$iTimer = _Timer_SetTimer($hGUI, 100, "_Timer")

While 1
    Sleep(10)
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    ; this is a 15+ second load that
    ; kills the GUI's responsiveness
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    For $i = 1 To $listArray[0]
        If ProcessExists($listArray[$i]) Then
            ProcessClose($listArray[$i])
            ;logging function goes here
            ProcessWaitClose($listArray[$i], 1)
            ;ProcessWaitClose is to prevent dozens
            ;of log entries while processes close
        EndIf
        Sleep(10) ; minimum sleep is 10?
    Next
    ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
WEnd

Func _Timer($hWnd, $Msg, $iIDTimer, $dwTime)
    #forceref $hWnd, $Msg, $iIDTimer, $dwTime
    ;end splash screen
    If WinActive("test-help", "Send an e-mail") Then SplashOff()

    If ProcessExists("notepad.exe") Then
        ProcessClose("notepad.exe")
        GUISetState(@SW_SHOW)
        GUICtrlSetState($Input1, $GUI_FOCUS)
        ProcessWaitClose("notepad.exe")
    EndIf
EndFunc

Func OnAutoItExit()
    _Timer_KillTimer($hGUI, $iTimer)
    ;_Timer_KillAllTimers($hGUI)
EndFunc

Func CountDown()
    For $i = ($time * 60) To 1 Step -1
        $h = ($i / 3600)
        $m = ($h - Int($h)) * 60
        $s = ($m - Int($m)) * 60
        ToolTip(Int($h) & "h " & Int($m) & "m " & Int($s) & "s", 0, 0)
        If Not ProcessExists("notepad.exe") Then ExitLoop
        Sleep(1000)
    Next
    ToolTip("")
    If ProcessExists("notepad.exe") Then
        ProcessClose("notepad.exe")
        ProcessWaitClose("notepad.exe")
    EndIf
EndFunc   ;==>CountDown

Func _Buttons()
    Switch @GUI_CtrlId
        Case $Button1
            $Ans = GUICtrlRead($Input1)
            GUICtrlSetData($Input1, "")
            GUICtrlSetState($Input1, $GUI_FOCUS)

            If $Ans = "close-test" Then Exit

            If $Ans = $psw Then
                GUISetState(@SW_HIDE)
                _Timer_KillTimer($hGUI, $iTimer)
                Run("notepad.exe")
                ProcessWait("notepad.exe", 5)
                CountDown()
                $iTimer = _Timer_SetTimer($hGUI, 100, "_Timer")
            EndIf
        Case $Button2
            GUISetState(@SW_HIDE)
        Case $Button3
            SplashTextOn("test-help", "Send an e-mail to...." & _
                    @CR & @CR & "Click on this screen to exit.", -1, 100, Default, Default, 18)
            GUICtrlSetState($Input1, $GUI_FOCUS)
            ;see "end splash screen"
    EndSwitch
EndFunc

Func _Events()
    Switch @GUI_CtrlId
        Case $GUI_EVENT_CLOSE
             GUISetState(@SW_HIDE)
    EndSwitch
EndFunc
Edited by rover

I see fascists...

Link to comment
Share on other sites

  • Developers

The OnEvent mode would not wait for any loop to be finished thus would make your gui to respond directly.

Only thing to consider is that the events are process consecutivly meaning that the Event-call to the Func needs to be finished before the next Event is fired.

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

The OnEvent mode would not wait for any loop to be finished thus would make your gui to respond directly.

Only thing to consider is that the events are process consecutivly meaning that the Event-call to the Func needs to be finished before the next Event is fired.

Jos

try increasing the value of sleep to 1000 in the for/next loop in the main while/wend loop

to simulate long processing time.

without the added timer the gui is unresponsive.

using timers is a way of getting around a blocking function.

I use this to display countdown numbers on standard AutoIt message box buttons

Edit: typo

Edited by rover

I see fascists...

Link to comment
Share on other sites

Thanks - it will be a bit before I can test on 2 weak computers. They are busy.

> ; minimum sleep is 10?

:-)

You know why I use Sleep(9) don't you?.

I'm on a laptop without a 10 keypad; so, the "(" and the "9" are the same key. :-)

I don't know if you were around during the days and days of discussion about folks wondering why Sleep(1) thru Sleep(10)-ish made little difference, but I don't think that things have changed. Sleep(1) still delays about like Sleep(10)... so my lazy Sleep(9) works the same.

Thanks all for the input.

Looks like I have some reading to do before diving into OnEvent.

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

  • Developers

try increasing the value of sleep to 1000 in the for/next loop in the main while/wend loop

to simulate long processing time.

without the added timer the gui is unresponsive.

using timers is a way of getting around a blocking function.

I use this to display countdown numbers on standard AutoIt message box buttons

Edit: typo

Not sure if you are saying you are in agreement or not but did you try creating a loop with sleep() and have the GUI Controls events fired in Opt("GUIOnEventMode",1) mode?

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Not sure if you are saying you are in agreement or not but did you try creating a loop with sleep() and have the GUI Controls events fired in Opt("GUIOnEventMode",1) mode?

Jos

@Jos

@herewasplato

yes, the above example uses oneventmode.

and the timer handles the closing notepad functions when the processwait loop is bogged down

the only problem is when the countdown function is running

the while/wend loop processwait for/next loop needs to be run from the for/next loop

in the countdown function, otherwise its stalled waiting for the countdown event to return.

a second script with WM_COPYDATA interscript communication would be better.

@herewasplato

Thanks - it will be a bit before I can test on 2 weak computers. They are busy.

> ; minimum sleep is 10?

:-)

You know why I use Sleep(9) don't you?.

I'm on a laptop without a 10 keypad; so, the "(" and the "9" are the same key. :-)

I don't know if you were around during the days and days of discussion about folks wondering why Sleep(1) thru Sleep(10)-ish made little difference, but I don't think that things have changed. Sleep(1) still delays about like Sleep(10)... so my lazy Sleep(9) works the same.

my Sleep goes to 11 :(

I vaguely recall reading posts about Sleep being 10

I started out using 5 as a minimum, It's just wishful thinking.

If I set it to 5, it will be 5 :P

Edit: misspelling

Edited by rover

I see fascists...

Link to comment
Share on other sites

  • 2 weeks later...

Man! Everything is stacking up against me in this thread... except the help has been good/informative.

1) I was a bad OP - I was so concerned with posting a small working sample that I failed to post the application/reason for the code. It kills IE and 15+ other "popular" browsers. Notepad represents IE in the sample code. Notepad/IE and the other 15+ browsers can stay open during the countdown function. That function is only called if the password is correct.

2) I post at the wrong times - when the forum goes down for maintenance - I wrote this post a few days ago and I thought that it went thru.

3) If it is not the forum going down, then it is my Wireless Access Point having fun randomly disconnecting me from the itself.

4) Silly life keeps getting in the way of fun with AutoIt. I've concluded that I don't own a house/lawn/car - they own me.

5) The job is a bummer right now - but at least I have one.

6) I can't get motivated to dive into this code again. Sorry, I've done little more than read over the suggested code. That does not mean that I'm not grateful or that the suggested code is not useful/helpful. It just means that I'll get back to this in the future. The code that I have works, but could be better.

...but thanks to all that have posted. Maybe my explanation of what the code is used for will help you understand when "browser processes" should be allowed to run and when they should be killed. Pretty much kill them 24/7 - but a good password equals a limited number of minutes to surf - then it is back to killing all browsers. If you know of a better way to password limit and time limit surfing, I'll listen; but the computers that this is installed on must stay connected to the LAN and Internet 24/7.

IE is the trigger browser

If you must surf with another browser:

Start IE

Enter the password

Leave IE open

Start your browser of choice.

Closing IE will kill all 15+ browsers.

The computers that this script runs on have a dedicated function and surfing porn is not one of them. IE is used to get some files via browser based e-mail accounts maybe once a month and to surf & order stuff every few months. Unfortunately, they run XP Home with no login password because the "dedicated function" is staffed by a wide variety of volunteers - teens and adults. I was asked to password protect IE and kill all other browsers (which I can not do) - so the control method was not my idea. All solutions must be free. Just like my volunteer labor is for this organization.

[size="1"][font="Arial"].[u].[/u][/font][/size]

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