Jump to content

Reduce CPU usage?


 Share

Recommended Posts

I have a makeshift internet browser. It works well, up until a website (such as Digg or Slashdot) is loaded, but then consumes around 30-40% of the cpu. Can any of you more experienced coders suggest something to reduce cpu with my current code?

CODE
#include <GUIConstants.au3>

#include <IE.au3>

#NoTrayIcon

_IEErrorHandlerRegister()

Global $oIE = ObjCreate("Shell.Explorer.2")

Global $ini = @ScriptDir & "\isurf.ini"

Global $read_height = IniRead($ini, "Display", "Height", "")

Global $read_width = IniRead($ini, "Display", "Width", "")

Global $read_homepage = IniRead($ini, "Settings", "Homepage", "")

Global $read_starthomepage = IniRead($ini, "Settings", "StartHomepage", "")

If $read_height = "" Then

$read_height = 600

$read_width = 800

EndIf

If $read_starthomepage = "" Then

$read_starthomepage = "1"

$read_homepage = "http://www.google.com"

EndIf

If $read_starthomepage = "0" Then $read_homepage = "about:none"

$gui_main = GUICreate("Internet Surfer", $read_width, $read_height+20, -1, -1, $WS_CLIPSIBLINGS + $WS_OVERLAPPEDWINDOW)

$file_menu = GUICtrlCreateMenu("File")

$fm_open = GUICtrlCreateMenuItem("Open...", $file_menu)

$fm_sep = GUICtrlCreateMenuItem("", $file_menu)

$fm_exit = GUICtrlCreateMenuItem("Exit", $file_menu)

$view_menu = GUICtrlCreateMenu("View")

$vm_status = GUICtrlCreateMenuItem("Status Bar", $view_menu)

GUICtrlSetState(-1, $GUI_CHECKED)

$tool_menu = GUICtrlCreateMenu("Tools")

$tm_pref = GUICtrlCreateMenuItem("Preferences...", $tool_menu)

$gui_iewindow = GUICtrlCreateObj($oIE, 0, 0, $read_width, $read_height - 45, $SS_SUNKEN)

GUICtrlSetResizing($gui_iewindow, $GUI_DOCKAUTO)

$address_bar_label = GUICtrlCreateLabel("Enter Address: ", 205, 562, 85, -1)

$address_bar_input = GUICtrlCreateInput($read_homepage, 280, 555, 200, -1)

$address_bar_go = GUICtrlCreateButton("Go >>", 495, 553, 50, -1, $BS_DEFPUSHBUTTON)

$address_bar_stop = GUICtrlCreateButton("Stop", 545, 553, 50, -1)

$status_bar = GUICtrlCreateLabel("Loading: " & $read_homepage & "...", 0, 584, $read_width, $read_height, BitOr($SS_SIMPLE, $SS_SUNKEN))

GUISetState(@SW_SHOW, $gui_main)

_IENavigate($oIE, $read_homepage)

$gui_pref = GUICreate("Preferences", 298, 186, 359, 232)

$group1 = GUICtrlCreateGroup("Settings", 16, 16, 265, 113)

$label_home = GUICtrlCreateLabel("Homepage:", 32, 40, 59, 17)

$input_add = GUICtrlCreateInput($read_homepage, 96, 40, 161, 21)

$label_start = GUICtrlCreateLabel("On Startup:", 32, 80, 58, 17)

$combo_startup = GUICtrlCreateCombo("", 96, 80, 161, 25)

If $read_starthomepage = "0" Then

GUICtrlSetData(-1, "Start with Homepage|Start with Blank Page", "Start with Blank Page")

Else

GUICtrlSetData(-1, "Start with Homepage|Start with Blank Page", "Start with Homepage")

EndIf

GUICtrlCreateGroup("", -99, -99, 1, 1)

$button_apply = GUICtrlCreateButton("Apply", 120, 144, 75, 25, 0)

$button_close = GUICtrlCreateButton("Close", 208, 144, 75, 25, 0)

GUISetState(@SW_HIDE, $gui_pref)

While 1

If _IEPropertyGet($oIE, "busy") Then

$url = _IEPropertyGet($oIE, "locationurl")

_StatusChange("Loading: " & $url & "...")

Else

_StatusChange("Done.")

EndIf

$msg = GUIGetMsg()

Switch $msg

Case $GUI_EVENT_CLOSE

If WinGetHandle('') = $gui_main Then

Exit

Else

GUISetState(@SW_HIDE, WinGetHandle(''))

EndIf

Case $fm_exit

Exit

Case $fm_open

Local $file = FileOpenDialog("Select file...", @ScriptDir, "All Files (*.*)")

If @error <> 1 Then

_StatusChange("Loading: " & $file & "...")

_IENavigate($oIE, $file)

EndIf

Case $vm_status

$size = WinGetPos("Internet Surfer")

If BitAnd(GUICtrlRead($vm_status), $GUI_CHECKED) = $GUI_CHECKED Then

GUICtrlSetState($vm_status, $GUI_UNCHECKED)

GUICtrlSetState($status_bar, $GUI_HIDE)

WinMove("Internet Surfer", "", $size[0], $size[1], $size[2], $size[3] - 15)

Else

GUICtrlSetState($vm_status, $GUI_CHECKED)

GUICtrlSetState($status_bar, $GUI_SHOW)

WinMove("Internet Surfer", "", $size[0], $size[1], $size[2], $size[3] + 15)

EndIf

Case $tm_pref

GUISetState(@SW_SHOW, $gui_pref)

Case $button_close

GUISetState(@SW_HIDE, $gui_pref)

Case $button_apply

$gethome = GUICtrlRead($input_add)

$getstarthome = GUICtrlRead($combo_startup)

If $getstarthome = "Start with Homepage" Then

IniWrite($ini, "Settings", "StartHomepage", "1")

Else

IniWrite($ini, "Settings", "StartHomepage", "0")

EndIf

IniWrite($ini, "Settings", "Homepage", $gethome)

GUISetState(@SW_HIDE, $gui_pref)

_StatusChange("Configuration written successfully.")

Sleep(1000)

Case $address_bar_go

$addy = GUICtrlRead($address_bar_input)

_StatusChange("Loading: " & $addy & "...")

_IENavigate($oIE, $addy)

_StatusChange("Done.")

Case $address_bar_stop

_IEAction($oIE, "stop")

EndSwitch

WEnd

Func _StatusChange($message)

GUICtrlSetData($status_bar, $message)

EndFunc

I think the problem lies within this portion:

If _IEPropertyGet($oIE, "busy") Then
    $url = _IEPropertyGet($oIE, "locationurl")
    _StatusChange("Loading: " & $url & "...")
Else
    _StatusChange("Done.")
EndIf

.. but I don't know how else to update the status bar with the information. Any suggestions?

Also, if any of you notice that when the Preferences gui is loaded, often the input will not be deleted upon pressing backspace.. any of you know why? The IE obj always keeps focus, and I haven't a clue how to stop it from recognizing "backspace" as "back".

Thanks for any help. :whistle:

Link to comment
Share on other sites

I would not suggest sleep in a GuiGetMsg loop. The problem is just too much going on with each loop cycle. I have added a ContinueLoop condition of messages from -50 to 0 to restart the loop as your not concerned for them as far as I can see.

$msg = GUIGetMsg()
Switch $msg
    Case $GUI_EVENT_CLOSE
        If WinGetHandle('') = $gui_main Then
            Exit
        Else
            GUISetState(@SW_HIDE, WinGetHandle(''))
        EndIf
    Case $fm_exit
        Exit
    Case -50 To 0
        ContinueLoop
    Case $fm_open
oÝ÷ Ù8^r^jÛazËZ®Ú¶¥¢l'méhÂÇȧµéì÷Ü(ºWlyézÛ^®Úzëw^­æ©j¢²Zv)à­«az»az|!z|¨º±®+#ºËbvX§©Ý².Ù趯zÚ(rí{ç$¶­rÝب«­¢+Ø)%}%AɽÁÉÑåÐ ÀÌØí½%°ÅÕ½ÐíÕÍäÅÕ½Ðì¤Q¡¸($ÀÌØíÕÉ°ô}%AɽÁÉÑåÐ ÀÌØí½%°ÅÕ½Ðí±½Ñ¥½¹ÕÉ°ÅÕ½Ðì¤(%}MÑÑÕÍ
¡¹ ÅÕ½Ðí1½¥¹èÅÕ½ÐìµÀìÀÌØíÕÉ°µÀìÅÕ½Ð츸¸ÅÕ½Ðì¤)±Í(%}MÑÑÕÍ
¡¹ ÅÕ½Ðí½¹¸ÅÕ½Ðì¤)¹%

There could be some other issues at fault, but fixing those first may make the issue go away with luck.

:whistle:

Link to comment
Share on other sites

I would not suggest sleep in a GuiGetMsg loop. The problem is just too much going on with each loop cycle. I have added a ContinueLoop condition of messages from -50 to 0 to restart the loop as your not concerned for them as far as I can see.

Case -50 To 0
        ContinueLoop
Excuse my ignorance, but, I don't get it. Case -50 ? :shocked:
Link to comment
Share on other sites

Excuse my ignorance, but, I don't get it. Case -50 ? :shocked:

GuiGetMsg() can release messages in the negative numerical area so adding a case condition in the block at a certain place can save the trouble of AutoIt going through the rest of the Switch block when there is no need, which saves cpu. Alternatively, sometimes these negative messages can be useful as when you click down on your mouse in the Gui, so for e.g., a -7 message may be released so you can act on that message to do some certain action.

-50 To 0 means anywhere between negative 50 and zero

:(

Edited by MHz
Link to comment
Share on other sites

GuiGetMsg() can release messages in the negative numerical area [...] so for e.g., a -7 message may be released so you can act on that message to do some certain action.

Great. So, GuiGetMsg() returns messages between -50 and 0 and -7 is the return value for a mouseclick ? But where are these values documented ? I can't see anything about this on the page with GuiGetMsg() in the helpfile...

I'm currently working on the following code:

While 1
    $msg = GuiGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
        Case $msg = ...
        Case $msg = ...
        Case Else
            ;;;
    EndSelect
    TrackTime($Media)
WEnd
oÝ÷ Ùh^­äëiÉg¢±¶¬Æ¥+©u«b©®+zËj¶§u©Zmé^M4ÓôÓzjëh×6GUICtrlSetData ( $songprogresspb, _SoundPos ( $Media, 2 )/_SoundLength ( $Media, 2 )*100 )
GUICtrlSetData ( $timelb, _SoundPos ( $Media, 1 ) & "/" & _SoundLength ( $Media, 1 ) )

The code is working, but the label flickers because it's updated too frequently. Once a second would be enough. But if I put a sleep into this, the whole GUI will become unresponsive. Could I involve the Case Else into this ?

Edited by birdofprey
Link to comment
Share on other sites

Great. So, GuiGetMsg() returns messages between -50 and 0 and -7 is the return value for a mouseclick ? But where are these values documented ? I can't see anything about this on the page with GuiGetMsg() in the helpfile...

I've seen messages no lower then about -20, but I add a starting point of a little more for the heck of it. These messages are undocumented as you should be using the advanced parameter of GuiGetMsg, if you want official use of the extra messages as you will see in the help file. The idea I discussed previously is just to use the extra messages that do get through without the need to be concerned about using GuiGetMsg advanced functionality.

The code is working, but the label flickers because it's updated too frequently. Once a second would be enough. But if I put a sleep into this, the whole GUI will become unresponsive. Could I involve the Case Else into this ?

I am not sure if you need a Progress Bar in your message loop, but if you do, then you may need to look at using TimerInit and TimerDiff to conditionally to set timed intervals for the progress.
Link to comment
Share on other sites

  • 3 months later...

Hi Emiel,

Are you are using Switch with the Case statement?

This test code works fine with AutoIt 3.2.5.0

ConsoleWrite(@AutoItVersion & @CRLF)

For $value = -1 To 1
    Switch $value
        Case -50 To 0
            MsgBox(0, 'Case is True', $value)
        Case Else
            MsgBox(0, 'Case Else is True', $value)
    EndSwitch
Next

:)

Link to comment
Share on other sites

Hi Emiel,

Are you are using Switch with the Case statement?

This test code works fine with AutoIt 3.2.5.0

ConsoleWrite(@AutoItVersion & @CRLF)

For $value = -1 To 1
    Switch $value
        Case -50 To 0
            MsgBox(0, 'Case is True', $value)
        Case Else
            MsgBox(0, 'Case Else is True', $value)
    EndSwitch
Next

:)

Hi Mhz,

indeed it was missing .. but it was late .. so .. i was sleepy...

Thnx

Emiel

Best regards,Emiel Wieldraaijer

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