Jump to content
Sign in to follow this  
Baritonomarchetto

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

Recommended Posts

Baritonomarchetto

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!

Share this post


Link to post
Share on other sites
water

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 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
water

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 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Baritonomarchetto

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

Share this post


Link to post
Share on other sites
water

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 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
water

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 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Baritonomarchetto

Thank you a lot :guitar:

Share this post


Link to post
Share on other sites
water

Glad to be of service :D


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
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
Sign in to follow this  

×