Jump to content

GUI Script to load a program - how to reduce CPU usage


Recommended Posts

Hi all,

I wrote a little script intended to show up a GUI with a list you can select a program to load from.

The problem is that the selected program, once loaded, is heavily slowed down so the script must be "heavy" while running in the background...

here is the code:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include "GUIScrollbars_Ex.au3"
#include <GuiListView.au3>
#include <Misc.au3>

Local $hDLL = DllOpen("user32.dll")
Local $hGUI

$hGUI = GUICreate("Select Program To Run", 250, 150,-1,-1,$WS_POPUP+$WS_BORDER)
GUISetBkColor(0x3790e8)

$listview = GUICtrlCreateListView("Select Program - press 1 to Run", 0, 0, 250,150, $LVS_SHOWSELALWAYS)
_GUICtrlListView_SetColumnWidth($listview, 0, 245)
GUICtrlSetBkColor(-1,0x3790e8)
$P1= GUICtrlCreateListViewItem("Program #1", $listview); ID 4
GUICtrlSetColor (-1, 0xFFFFFF)
$P2= GUICtrlCreateListViewItem("Program #2", $listview); ID 5
GUICtrlSetColor (-1, 0xFFFFFF)
$P3= GUICtrlCreateListViewItem("Program #3", $listview); ID 6
GUICtrlSetColor (-1, 0xFFFFFF)
$win= GUICtrlCreateListViewItem("Exit to Windows", $listview); ID 7
GUICtrlSetColor (-1, 0xFFFFFF)
$shutdown= GUICtrlCreateListViewItem("Shut Down PC", $listview); ID 8
GUICtrlSetColor (-1, 0xFFFFFF)

GUISetState(@SW_SHOW) ;shows the gui/menu/list

While 1
$GUIlistitem = GUICtrlRead($listView); reads the ID of the selected item in the list
;SplashTextOn ( "title", $GUIlistitem)
if Not (ProcessExists("P1.exe") Or ProcessExists("P2.exe") Or ProcessExists("P3.exe")) Then
If $GUIlistitem==4 And _IsPressed(31) Then ;31 -> Key 1
RunWait("C:/Programs/P1.exe")
nocycle()
EndIf
If $GUIlistitem==5 And _IsPressed(31) Then ;31 -> Key 1
RunWait("C:/Programs/P2.exe")
nocycle()
EndIf
If $GUIlistitem==6 And _IsPressed(31) Then ;31 -> Key 1
RunWait("C:/Programs/P3.exe")
nocycle()
EndIf
If $GUIlistitem==7 And _IsPressed(31) Then ;31 -> Key 1
Exit
nocycle()
EndIf
If $GUIlistitem==8 And _IsPressed(31) Then ;31 -> Key 1
Shutdown(1)
nocycle()
EndIf
Sleep(200)
EndIf
WEnd

Func nocycle()
While _IsPressed(31, $hDLL)
Sleep(500)
WEnd
EndFunc

DllClose($hDLL)

I am not a coder so there should be something of macroscopic i cannot see... :sweating:

If you have some idea on how to improve this "badly-drawn-boy" please tell me

Thanks for the help!

Link to comment
Share on other sites

To get the pressed key in a GUI you don't use _ISPressed but GUIGetmsg to check for a button (needs to be created in your GUI). For an example check function GUICreate in the help file.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Example:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>

Global $hGUI = GUICreate("Select Program To Run", 250, 150, -1, -1, $WS_POPUP + $WS_BORDER)
GUISetBkColor(0x3790e8)
Global $listview = GUICtrlCreateListView("Select Program - press 1 to Run", 0, 0, 250, 150, $LVS_SHOWSELALWAYS)
_GUICtrlListView_SetColumnWidth($listview, 0, 245)
GUICtrlSetBkColor(-1, 0x3790e8)
Global $P1 = GUICtrlCreateListViewItem("Program #1", $listview); ID 4
GUICtrlSetColor(-1, 0xFFFFFF)
$P2 = GUICtrlCreateListViewItem("Program #2", $listview); ID 5
GUICtrlSetColor(-1, 0xFFFFFF)
$P3 = GUICtrlCreateListViewItem("Program #3", $listview); ID 6
GUICtrlSetColor(-1, 0xFFFFFF)
$win = GUICtrlCreateListViewItem("Exit to Windows", $listview); ID 7
GUICtrlSetColor(-1, 0xFFFFFF)
$shutdown = GUICtrlCreateListViewItem("Shut Down PC", $listview); ID 8
GUICtrlSetColor(-1, 0xFFFFFF)
$Start = GUICtrlCreateDummy()
Global $AccelKeys[1][2] = [["1", $Start]]
GUISetAccelerators($AccelKeys)
GUISetState(@SW_SHOW) ;shows the gui/menu/list

While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    If $msg = $Start Then
        $GUIlistitem = GUICtrlRead($listview); reads the ID of the selected item in the list
        Switch $GUIlistitem
            Case 4
                If Not ProcessExists("P1.exe") Then RunWait("C:\Programs\P1.exe")
            Case 5
                If Not ProcessExists("P2.exe") Then RunWait("C:\Programs\P2.exe")
            Case 6
                If Not ProcessExists("P3.exe") Then RunWait("C:\Programs\P3.exe")
            Case 7
                Exit
            Case 8
                Shutdown(1)
        EndSwitch
    EndIf
WEnd

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Uh, thank you very much for the help!

just a question: why only $P1 in set as Global variable and $P>1 not?

EDIT: another Q: will "RunWait" prevent other programs to run while one is running or should i specify:

If Not (ProcessExists("P1.exe") Or ProcessExists("P2.exe") or ProcessExists("P3.exe")) Then RunWait("C:\Programs\P1.exe")
Edited by Baritonomarchetto
Link to comment
Share on other sites

Globals are not needed. Stripped down version:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>

GUICreate("Select Program To Run", 250, 150, -1, -1, $WS_POPUP + $WS_BORDER)
GUISetBkColor(0x3790e8)
Global $listview = GUICtrlCreateListView("Select Program - press 1 to Run", 0, 0, 250, 150, $LVS_SHOWSELALWAYS)
_GUICtrlListView_SetColumnWidth($listview, 0, 245)
GUICtrlSetBkColor(-1, 0x3790e8)
GUICtrlCreateListViewItem("Program #1", $listview); ID 4
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlCreateListViewItem("Program #2", $listview); ID 5
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlCreateListViewItem("Program #3", $listview); ID 6
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlCreateListViewItem("Exit to Windows", $listview); ID 7
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlCreateListViewItem("Shut Down PC", $listview); ID 8
GUICtrlSetColor(-1, 0xFFFFFF)
$Start = GUICtrlCreateDummy()
Global $AccelKeys[1][2] = [["1", $Start]]
GUISetAccelerators($AccelKeys)
GUISetState(@SW_SHOW) ;shows the gui/menu/list

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $Start
            $GUIlistitem = GUICtrlRead($listview); reads the ID of the selected item in the list
            Switch $GUIlistitem
                Case 4
                    If Not ProcessExists("P1.exe") Then RunWait("C:\Programs\P1.exe")
                Case 5
                    If Not ProcessExists("P2.exe") Then RunWait("C:\Programs\P2.exe")
                Case 6
                    If Not ProcessExists("P3.exe") Then RunWait("C:\Programs\P3.exe")
                Case 7
                    Exit
                Case 8
                    Shutdown(1)
        EndSwitch
    EndSelect
WEnd

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

EDIT: another Q: will "RunWait" prevent other programs to run while one is running or should i specify:

RunWait means: AutoIt will wait until Px.exe has finished. This also means: You can't exit the GUI until the program you called has ended.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Glad to be of service :D

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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