Jump to content
Sign in to follow this  
hentaiw

Script consumes too much memory, please help me find where.

Recommended Posts

hentaiw

Here it's my whole script, still can't figure out where the fault is, After one afternoon of usage, it aggregates and takes up over 500 MB of RAM.

#Thanks
Edited by hentaiw

Share this post


Link to post
Share on other sites
Newb

What about trying to put some MsgBox or ToolTips or ConsoleWrites in various places (loops, etc etc.) to check the memory usage from time to time and identify where the memory filling happens?


I'm a compulsive poster. When I post something, come to read it at least 5 minutes later after the posting, because I will edit it. I edited even this signature a few minutes later after I wrote it.

Share this post


Link to post
Share on other sites
tonycst

What about scripts that dont continuously increase their consumption but are using many megabites already with very little going on ?

I have a script

#RequireAdmin
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=MonOff.ico
#AutoIt3Wrapper_Outfile=..\Laptop Monitor OFF.exe
#AutoIt3Wrapper_Compression=0
#AutoIt3Wrapper_UseUpx=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiButton.au3>
#include <SendMessage.au3>
#include <ButtonConstants.au3>
FileInstall("MonOff.ico", @TempDir & "\MonOff.ico",1)
FileInstall("Restart.ico", @TempDir & "\Restart.ico",1)
FileInstall("ShutDown.ico", @TempDir & "\ShutDown.ico",1)
Opt("WinTitleMatchMode", 4)
Opt("TrayOnEventMode", 1 + 2)
Opt("TrayAutoPause", 0)
Global $GUI,$GUI1,$GUI2, $TestGUI,$TestGUI1,$TestGUI2
TrayCreateItem("Settings")
TrayItemSetOnEvent(-1, "_Settings")
If IniRead(@ScriptDir & "\Settings.ini", "ButtonPosition", "X", "") = "" Then
    _Settings()
Else
    _MAIN()
EndIf
Func _Settings()
    GUIDelete($GUI)
    GUIDelete($GUI1)
    GUIDelete($GUI2)

    $SetingsGUI = GUICreate("Settings", 210, 190)
    GUISetState(@SW_SHOW, $SetingsGUI)
    GUICtrlCreateGroup("Coordinates", 5, 5, 200, 60)
    GUICtrlCreateLabel("How many pixels from the LEFT", 10, 23, 150, 20)
    $Xinput = GUICtrlCreateInput(IniRead(@ScriptDir & "\Settings.ini", "ButtonPosition", "X", ""), 160, 20, 40, 20)
    GUICtrlCreateLabel("How many pixels from the TOP", 10, 43, 150, 20)
    $Yinput = GUICtrlCreateInput(IniRead(@ScriptDir & "\Settings.ini", "ButtonPosition", "Y", ""), 160, 40, 40, 20)
    $try = GUICtrlCreateButton("Try", 5, 70, 100, 25)
    $save = GUICtrlCreateButton("Save", 105, 70, 100, 25)

    GUICtrlCreateGroup ("Select Buttons to show",5,145,200,40)
    $EnableGUIMonOff = GUICtrlCreateCheckbox ("Screen Off",10,160)
    GUICtrlSetState (-1,$GUI_Checked)
    $EnableGUIRestart = GUICtrlCreateCheckbox ("Reboot",85,160)
    GUICtrlSetState (-1,$GUI_Checked)
    $EnableGUIShutDown = GUICtrlCreateCheckbox ("Turn Off",145,160)
    GUICtrlSetState (-1,$GUI_Checked)

    $EnableStartUp = GUICtrlCreateButton("Enable Start with Windows", 0, 96, 210, 25)
    If RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", "MonitorOff") > "" Then GUICtrlSetData($EnableStartUp, "Disable Start with Windows")

    $TranspSet = GUICtrlCreateButton("Set visibility from 0-255", 0, 120, 180, 25)
    $value = GUICtrlCreateInput(IniRead(@ScriptDir & "\Settings.ini", "ButtonSettings", "Visibility", ""), 180, 120, 30, 25)
    If IniRead(@ScriptDir & "\Settings.ini", "ButtonSettings", "Visibility", "") = "" Then
        GUICtrlSetData($value, 255)
        IniWrite(@ScriptDir & "\Settings.ini", "ButtonSettings", "Visibility", "255")
    EndIf
    While 1
        $nmsg = GUIGetMsg()
        If $nmsg = $GUI_EVENT_CLOSE Then
            GUIDelete($TestGUI)
            GUIDelete($TestGUI1)
            GUIDelete($TestGUI2)

            GUIDelete($SetingsGUI)
            _MAIN()
        EndIf
        If $nmsg = $try Then
            $X=  GUICtrlRead($Xinput)
            $Y = GUICtrlRead($Yinput)

            If GUICtrlRead ($EnableGUIMonOff) = $GUI_Checked Then
                $TestGUI = GUICreate("MonOff", 128, 128, $X, $Y, $WS_POPUP, $WS_EX_TOPMOST)
                Dim $TestOFF = GUICtrlCreateButton("OFF", 0, 0, 128, 128, $BS_ICON)
                GUICtrlSetImage(-1, @TempDir & "\MonOff.ico")
                GUISetState(@SW_SHOW, $TestGUI)
                WinSetTrans($TestGUI, "", IniRead(@ScriptDir & "\Settings.ini", "ButtonSettings", "Visibility", ""))
            EndIf
            If GUICtrlRead ($EnableGUIRestart) = $GUI_Checked Then
                $TestGUI1 = GUICreate("Restart", 128, 128, $X, $Y+128, $WS_POPUP, $WS_EX_TOPMOST)
                Dim $TestOFF = GUICtrlCreateButton("OFF", 0, 0, 128, 128, $BS_ICON)
                GUICtrlSetImage(-1, @TempDir & "\Restart.ico")
                GUISetState(@SW_SHOW, $TestGUI1)
                WinSetTrans($TestGUI1, "", IniRead(@ScriptDir & "\Settings.ini", "ButtonSettings", "Visibility", ""))
            EndIf
            If GUICtrlRead ($EnableGUIShutDown) = $GUI_Checked Then
                $TestGUI2 = GUICreate("ShutDown", 128, 128, $X, $Y+256, $WS_POPUP, $WS_EX_TOPMOST)
                Dim $TestOFF = GUICtrlCreateButton("OFF", 0, 0, 128, 128, $BS_ICON)
                GUICtrlSetImage(-1, @TempDir & "\ShutDown.ico")
                GUISetState(@SW_SHOW, $TestGUI2)
                WinSetTrans($TestGUI2, "", IniRead(@ScriptDir & "\Settings.ini", "ButtonSettings", "Visibility", ""))
            EndIf


            MsgBox(64, "Preview", "Press OK when done")
            GUIDelete($TestGUI)
            GUIDelete($TestGUI1)
            GUIDelete($TestGUI2)
        EndIf
        If $nmsg = $save Then
            IniWrite(@ScriptDir & "\Settings.ini", "ButtonPosition", "X", GUICtrlRead($Xinput))
            IniWrite(@ScriptDir & "\Settings.ini", "ButtonPosition", "Y", GUICtrlRead($Yinput))
            If GUICtrlRead ($EnableGUIMonOff) = $GUI_Checked Then IniWrite(@ScriptDir & "\Settings.ini", "Display_Elements", "MonOff", "Yes")
            If GUICtrlRead ($EnableGUIRestart) = $GUI_Checked Then IniWrite(@ScriptDir & "\Settings.ini", "Display_Elements", "Restart", "Yes")
            If GUICtrlRead ($EnableGUIShutDown) = $GUI_Checked Then IniWrite(@ScriptDir & "\Settings.ini", "Display_Elements", "ShutDown", "Yes")

            MsgBox("32", "Success !", "Saved")
            GUIDelete($TestGUI)
            GUIDelete($SetingsGUI)
            _MAIN()
        EndIf
        If $nmsg = $EnableStartUp Then
            If RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", "MonitorOff") > "" Then
                RegDelete("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", "MonitorOff")
                GUICtrlSetData($EnableStartUp, "Enable Start with Windows")
            Else
                RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", "MonitorOff", "REG_SZ", @ScriptFullPath)
                GUICtrlSetData($EnableStartUp, "Disable Start with Windows")
            EndIf
        EndIf
        If $nmsg = $TranspSet Then
            IniWrite(@ScriptDir & "\Settings.ini", "ButtonSettings", "Visibility", GUICtrlRead($value))
            MsgBox(0, "Success !", "Saved" & @CRLF & "Please preview.")
        EndIf
    WEnd
EndFunc   ;==>_Settings

Func _MAIN()
    Local $OFF,$Restart,$Shutdown
    $HiddenGUI = GUICreate("")
    $MainX = IniRead(@ScriptDir & "\Settings.ini", "ButtonPosition", "X", "")
    $MainY =  IniRead(@ScriptDir & "\Settings.ini", "ButtonPosition", "Y", "")

    If IniRead(@ScriptDir & "\Settings.ini", "Display_Elements", "MonOff", "") = "Yes" Then
        $GUI = GUICreate("ScreenOFF", 128, 128, $MainX ,$MainY , $WS_POPUP, $WS_EX_TOPMOST, $HiddenGUI)
        Dim $OFF = GUICtrlCreateButton("", 0, 0, 128, 128, $BS_ICON)
        GUICtrlSetImage(-1, @TempDir & "\MonOff.ico")
    EndIf
    If IniRead(@ScriptDir & "\Settings.ini", "Display_Elements", "Restart", "") = "Yes" Then
        $GUI1 = GUICreate("Restart", 128, 128, $MainX ,$MainY + 128 , $WS_POPUP, $WS_EX_TOPMOST, $HiddenGUI)
        Dim $Restart = GUICtrlCreateButton("", 0, 0, 128, 128, $BS_ICON)
        GUICtrlSetImage(-1, @TempDir & "\Restart.ico")
    EndIf
    If IniRead(@ScriptDir & "\Settings.ini", "Display_Elements", "ShutDown", "") = "Yes" Then
        $GUI2 = GUICreate("ShutDown", 128, 128, $MainX ,$MainY + 256 , $WS_POPUP, $WS_EX_TOPMOST, $HiddenGUI)
        Dim $Shutdown = GUICtrlCreateButton("", 0, 0, 128, 128, $BS_ICON)
        GUICtrlSetImage(-1, @TempDir & "\ShutDown.ico")
    EndIf
    If IniRead(@ScriptDir & "\Settings.ini", "Display_Elements", "MonOff", "") = "" and IniRead(@ScriptDir & "\Settings.ini", "Display_Elements", "Restart", "") = "" And IniRead(@ScriptDir & "\Settings.ini", "Display_Elements", "ShutDown", "") = "" Then
        MsgBox (16,"Error",'Nothing to show. Please access tray menu to set which buttons to show and click "Save"')
        _Settings()
    EndIf
    GUISetState(@SW_SHOW, $GUI)
    GUISetState(@SW_SHOW, $GUI1)
    GUISetState(@SW_SHOW, $GUI2)

    WinSetTrans($GUI, "", IniRead(@ScriptDir & "\Settings.ini", "ButtonSettings", "Visibility", ""))
    WinSetTrans($GUI1, "", IniRead(@ScriptDir & "\Settings.ini", "ButtonSettings", "Visibility", ""))
    WinSetTrans($GUI2, "", IniRead(@ScriptDir & "\Settings.ini", "ButtonSettings", "Visibility", ""))
    $WM_SYSCommand1 = 274
    $SC_MonitorPower = 61808
    $Power_On = -1
    $Power_Off = 2
    $HWND = WinGetHandle("classname=Progman")

    While 1
        $msg = GUIGetMsg()
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
        If $msg = $OFF Then
            DllCall("user32.dll", "int", "SendMessage", "hwnd", $HWND, "int", $WM_SYSCommand1, "int", $SC_MonitorPower, "int", $Power_Off)
        EndIf
        If $msg = $Restart Then Shutdown (2)
        If $msg = $Shutdown Then Shutdown(8)
    WEnd
EndFunc   ;==>_MAIN

Not whole lot going on yet it uses 13.xxx KB

I have scripts that are much more complicated and longer that use only 4.xxx KB

Share this post


Link to post
Share on other sites
BrewManNH

You've got a recursion problem in that script, you call the Settings function, and then when done using it, you call the Main function rather than using Return.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
tonycst

am not clear on what return does. I can read help file all day and understand nothing about it.

How would it work if i had 2 functions and return would be in function #2. Would it return to #2 or #1 ?

What exactly does it do. Does it end function like EndIf and returns to its beginning or just ends it ?

 

Thanks

Share this post


Link to post
Share on other sites
BrewManNH

The Return keyword will, as its name implies, return from a function back to where ever it was called from. In your case, you call the settings function, then you should use the Return function rather than jumping back and rerunning the Main function.

http://www.autoitscript.com/wiki/Recursion


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
tonycst

u mean in line 112 ?

If i put Return instead of _MAIN(), i will not be able to open Settings window again.

I jump from one function to another for that reason.

It consumes 30+megs by running _MAIN() alone without touching anything.

Share this post


Link to post
Share on other sites
ripdad

Tracked it down to an include -- which has includes -- and which they have includes.

#include <GuiButton.au3>; <============ comment this include and you'll see the difference

Local $gui = GUICreate('test', 128, 128)
GUISetState(@SW_SHOW, $gui)

Do
Until GUIGetMsg() = -3
GUIDelete($gui)
Exit

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
tonycst

All i see after commenting this include is INABILITY to open settings dialogue more then once.

Share this post


Link to post
Share on other sites
ripdad

All I did was show you where the extra memory was consumed at.

BrewManNH was right about a recursion problem in your script.

If you can, try to structure your script like this...

; Includes
; Declarations
; Gui
; Buttons, etc.
; Show

;main loop
While 1
    $sMsg = GUIGetMsg()
    If $sMsg = $GUI_EVENT_CLOSE Then
       ExitLoop
    ElseIf $sMsg = $button1 Then
        _somefunction1(); call function from here
    ElseIf $sMsg = $button2 Then
       _somefunction2()
    Endif
   ; and so on....

WEnd

Func _somefunction1()
    ; do stuff
    Return;  use "Return" to go back to the main loop if needed
EndFunc

Func _somefunction2()
    ; do stuff
    Return
EndFunc

It's a good idea to return from a function at some point. Otherwise, you're stuck in them with a possible recursion.

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
tonycst

I see.

It starts to make sense.

So even if i do erase entire GUI previously created, still could be a memory leak ?

I understand that it could increase overtime but its high at the beginning on its initial function for displaying 3 GUI's on the screen with 1 icon each. Not much yet lots of resources are used.

Just wondered if that was normal.

Share this post


Link to post
Share on other sites
ripdad

Did you restructure your script?

If so, please post it, so we can determine other possible problems.


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
tonycst

#RequireAdmin
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=MonOff.ico
#AutoIt3Wrapper_Outfile=..\Laptop Monitor OFF.exe
#AutoIt3Wrapper_Compression=0
#AutoIt3Wrapper_UseUpx=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiButton.au3>
#include <SendMessage.au3>
#include <ButtonConstants.au3>
FileInstall("MonOff.ico", @TempDir & "\MonOff.ico",1)
FileInstall("Restart.ico", @TempDir & "\Restart.ico",1)
FileInstall("ShutDown.ico", @TempDir & "\ShutDown.ico",1)
Opt("WinTitleMatchMode", 4)
Opt("TrayOnEventMode", 1 + 2)
Opt("TrayAutoPause", 0)
Global $GUI,$GUI1,$GUI2, $TestGUI,$TestGUI1,$TestGUI2
TrayCreateItem("Settings")
TrayItemSetOnEvent(-1, "_Settings")
If IniRead(@ScriptDir & "\Settings.ini", "ButtonPosition", "X", "") = "" Then
    _Settings()
Else
    _MAIN()
EndIf

Func _Settings()
    GUIDelete($GUI)
    GUIDelete($GUI1)
    GUIDelete($GUI2)

    $SetingsGUI = GUICreate("Settings", 210, 190)
    GUISetState(@SW_SHOW, $SetingsGUI)
    GUICtrlCreateGroup("Coordinates", 5, 5, 200, 60)
    GUICtrlCreateLabel("How many pixels from the LEFT", 10, 23, 150, 20)
    $Xinput = GUICtrlCreateInput(IniRead(@ScriptDir & "\Settings.ini", "ButtonPosition", "X", ""), 160, 20, 40, 20)
    GUICtrlCreateLabel("How many pixels from the TOP", 10, 43, 150, 20)
    $Yinput = GUICtrlCreateInput(IniRead(@ScriptDir & "\Settings.ini", "ButtonPosition", "Y", ""), 160, 40, 40, 20)
    $try = GUICtrlCreateButton("Try", 5, 70, 100, 25)
    $save = GUICtrlCreateButton("Save", 105, 70, 100, 25)

    GUICtrlCreateGroup ("Select Buttons to show",5,145,200,40)
    $EnableGUIMonOff = GUICtrlCreateCheckbox ("Screen Off",10,160)
    If IniRead (@ScriptDir & "\Settings.ini", "Display_Elements", "MonOff", "") = "Yes" Then GUICtrlSetState (-1,$GUI_Checked)
    $EnableGUIRestart = GUICtrlCreateCheckbox ("Reboot",85,160)
    If IniRead (@ScriptDir & "\Settings.ini", "Display_Elements", "Restart", "") = "Yes" Then GUICtrlSetState (-1,$GUI_Checked)
    $EnableGUIShutDown = GUICtrlCreateCheckbox ("Turn Off",145,160)
    If IniRead (@ScriptDir & "\Settings.ini", "Display_Elements", "ShutDown", "") = "Yes" Then GUICtrlSetState (-1,$GUI_Checked)

    $EnableStartUp = GUICtrlCreateButton("Enable Start with Windows", 0, 96, 210, 25)
    If RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", "MonitorOff") > "" Then GUICtrlSetData($EnableStartUp, "Disable Start with Windows")

    $TranspSet = GUICtrlCreateButton("Set visibility from 0-255", 0, 120, 180, 25)
    $value = GUICtrlCreateInput(IniRead(@ScriptDir & "\Settings.ini", "ButtonSettings", "Visibility", ""), 180, 120, 30, 25)
    If IniRead(@ScriptDir & "\Settings.ini", "ButtonSettings", "Visibility", "") = "" Then
        GUICtrlSetData($value, 255)
        IniWrite(@ScriptDir & "\Settings.ini", "ButtonSettings", "Visibility", "255")
    EndIf
    While 1
        $nmsg = GUIGetMsg()
        If $nmsg = $GUI_EVENT_CLOSE Then
            GUIDelete($TestGUI)
            GUIDelete($TestGUI1)
            GUIDelete($TestGUI2)

            GUIDelete($SetingsGUI)
            return
            ;_MAIN()
        EndIf
        If $nmsg = $try Then
            $X=  GUICtrlRead($Xinput)
            $Y = GUICtrlRead($Yinput)
            If GUICtrlRead ($EnableGUIMonOff) = $GUI_Checked Then
                $TestGUI = GUICreate("MonOff", 128, 128, $X, $Y, $WS_POPUP, $WS_EX_TOPMOST)
                Dim $TestOFF = GUICtrlCreateButton("OFF", 0, 0, 128, 128, $BS_ICON)
                GUICtrlSetImage(-1, @TempDir & "\MonOff.ico")
                GUISetState(@SW_SHOW, $TestGUI)
                WinSetTrans($TestGUI, "", IniRead(@ScriptDir & "\Settings.ini", "ButtonSettings", "Visibility", ""))
            EndIf
            If GUICtrlRead ($EnableGUIRestart) = $GUI_Checked Then
                $TestGUI1 = GUICreate("Restart", 128, 128, $X, $Y+128, $WS_POPUP, $WS_EX_TOPMOST)
                Dim $TestOFF = GUICtrlCreateButton("OFF", 0, 0, 128, 128, $BS_ICON)
                GUICtrlSetImage(-1, @TempDir & "\Restart.ico")
                GUISetState(@SW_SHOW, $TestGUI1)
                WinSetTrans($TestGUI1, "", IniRead(@ScriptDir & "\Settings.ini", "ButtonSettings", "Visibility", ""))
            EndIf
            If GUICtrlRead ($EnableGUIShutDown) = $GUI_Checked Then
                $TestGUI2 = GUICreate("ShutDown", 128, 128, $X, $Y+256, $WS_POPUP, $WS_EX_TOPMOST)
                Dim $TestOFF = GUICtrlCreateButton("OFF", 0, 0, 128, 128, $BS_ICON)
                GUICtrlSetImage(-1, @TempDir & "\ShutDown.ico")
                GUISetState(@SW_SHOW, $TestGUI2)
                WinSetTrans($TestGUI2, "", IniRead(@ScriptDir & "\Settings.ini", "ButtonSettings", "Visibility", ""))
            EndIf
            MsgBox(64, "Preview", "Press OK when done")
            GUIDelete($TestGUI)
            GUIDelete($TestGUI1)
            GUIDelete($TestGUI2)
            Return
        EndIf
        If $nmsg = $save Then
            IniWrite(@ScriptDir & "\Settings.ini", "ButtonPosition", "X", GUICtrlRead($Xinput))
            IniWrite(@ScriptDir & "\Settings.ini", "ButtonPosition", "Y", GUICtrlRead($Yinput))
            If GUICtrlRead ($EnableGUIMonOff) = $GUI_Checked Then
                IniWrite(@ScriptDir & "\Settings.ini", "Display_Elements", "MonOff", "Yes")
            Else
                IniWrite(@ScriptDir & "\Settings.ini", "Display_Elements", "MonOff", "No")
            EndIf
            If GUICtrlRead ($EnableGUIRestart) = $GUI_Checked Then
                IniWrite(@ScriptDir & "\Settings.ini", "Display_Elements", "Restart", "Yes")
            Else
                IniWrite(@ScriptDir & "\Settings.ini", "Display_Elements", "Restart", "No")
            EndIf
            If GUICtrlRead ($EnableGUIShutDown) = $GUI_Checked Then
                IniWrite(@ScriptDir & "\Settings.ini", "Display_Elements", "ShutDown", "Yes")
            Else
                IniWrite(@ScriptDir & "\Settings.ini", "Display_Elements", "ShutDown", "No")
            EndIf
            MsgBox("32", "Success !", "Saved")
            GUIDelete($TestGUI)
            GUIDelete($SetingsGUI)
            Return
        EndIf
        If $nmsg = $EnableStartUp Then
            If RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", "MonitorOff") > "" Then
                RegDelete("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", "MonitorOff")
                GUICtrlSetData($EnableStartUp, "Enable Start with Windows")
            Else
                RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", "MonitorOff", "REG_SZ", @ScriptFullPath)
                GUICtrlSetData($EnableStartUp, "Disable Start with Windows")
            EndIf
        EndIf
        If $nmsg = $TranspSet Then
            IniWrite(@ScriptDir & "\Settings.ini", "ButtonSettings", "Visibility", GUICtrlRead($value))
            MsgBox(0, "Success !", "Saved" & @CRLF & "Please preview.")
        EndIf
    WEnd
EndFunc   ;==>_Settings

Func _MAIN()
    Local $OFF,$Restart,$Shutdown
    $HiddenGUI = GUICreate("")
    $MainX = IniRead(@ScriptDir & "\Settings.ini", "ButtonPosition", "X", "")
    $MainY =  IniRead(@ScriptDir & "\Settings.ini", "ButtonPosition", "Y", "")

    If IniRead(@ScriptDir & "\Settings.ini", "Display_Elements", "MonOff", "") = "Yes" Then
        $GUI = GUICreate("ScreenOFF", 128, 128, $MainX ,$MainY , $WS_POPUP, $WS_EX_TOPMOST, $HiddenGUI)
        Dim $OFF = GUICtrlCreateButton("", 0, 0, 128, 128, $BS_ICON)
        GUICtrlSetImage(-1, @TempDir & "\MonOff.ico")
    EndIf
    If IniRead(@ScriptDir & "\Settings.ini", "Display_Elements", "Restart", "") = "Yes" Then
        $GUI1 = GUICreate("Restart", 128, 128, $MainX ,$MainY + 128 , $WS_POPUP, $WS_EX_TOPMOST, $HiddenGUI)
        Dim $Restart = GUICtrlCreateButton("", 0, 0, 128, 128, $BS_ICON)
        GUICtrlSetImage(-1, @TempDir & "\Restart.ico")
    EndIf
    If IniRead(@ScriptDir & "\Settings.ini", "Display_Elements", "ShutDown", "") = "Yes" Then
        $GUI2 = GUICreate("ShutDown", 128, 128, $MainX ,$MainY + 256 , $WS_POPUP, $WS_EX_TOPMOST, $HiddenGUI)
        Dim $Shutdown = GUICtrlCreateButton("", 0, 0, 128, 128, $BS_ICON)
        GUICtrlSetImage(-1, @TempDir & "\ShutDown.ico")
    EndIf
    If IniRead(@ScriptDir & "\Settings.ini", "Display_Elements", "MonOff", "") = "" and IniRead(@ScriptDir & "\Settings.ini", "Display_Elements", "Restart", "") = "" And IniRead(@ScriptDir & "\Settings.ini", "Display_Elements", "ShutDown", "") = "" Then
        MsgBox (16,"Error",'Nothing to show. Please access tray menu to set which buttons to show and click "Save"')
        return
    EndIf
    GUISetState(@SW_SHOW, $GUI)
    GUISetState(@SW_SHOW, $GUI1)
    GUISetState(@SW_SHOW, $GUI2)

    WinSetTrans($GUI, "", IniRead(@ScriptDir & "\Settings.ini", "ButtonSettings", "Visibility", ""))
    WinSetTrans($GUI1, "", IniRead(@ScriptDir & "\Settings.ini", "ButtonSettings", "Visibility", ""))
    WinSetTrans($GUI2, "", IniRead(@ScriptDir & "\Settings.ini", "ButtonSettings", "Visibility", ""))
    $WM_SYSCommand1 = 274
    $SC_MonitorPower = 61808
    $Power_On = -1
    $Power_Off = 2
    $HWND = WinGetHandle("classname=Progman")

    While 1
        $msg = GUIGetMsg()
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
        If $msg = $OFF Then
            DllCall("user32.dll", "int", "SendMessage", "hwnd", $HWND, "int", $WM_SYSCommand1, "int", $SC_MonitorPower, "int", $Power_Off)
        EndIf
        If IniRead(@ScriptDir & "\Settings.ini", "Display_Elements", "Restart", "") = "Yes" And $msg = $Restart Then Shutdown (2)
        If IniRead(@ScriptDir & "\Settings.ini", "Display_Elements", "ShutDown", "") = "Yes" And $msg = $Shutdown Then Shutdown(8)
    WEnd
EndFunc   ;==>_MAIN

There.

Now i am completely lost with this RETURN thing.

I want to jump from one function to another when one or another exists, jumping back and forth.

I dont see any other way of doing it but to call function from inside function.

I now have removed calling function from inside the function and replaced them all with Return.

I can now open settings dialog and start Preview just fine but cannot start _MAIN function after i close settings dialog or press Save.

When i close settings dialog, i would call _MAIN instead of RETURN, but that would be calling a function from inside the function which is not the way to go i guess.

At this point i just dont see any way of calling _MAIN function when Settings window gets closed. Once its closed, it does RETURN thing and then ...... well i simply dont what whats next.

 

Thanks

Share this post


Link to post
Share on other sites
ripdad

This will fix your first problem...

;change this
If IniRead(@ScriptDir & "\Settings.ini", "ButtonPosition", "X", "") = "" Then
    _Settings()
Else
    _MAIN()
EndIf

;to this
If IniRead(@ScriptDir & "\Settings.ini", "ButtonPosition", "X", "") = "" Then
    _Settings()
EndIf
_MAIN()

But... I have some questions for you to ponder...

Why do you need so many GUI's?

What's wrong with 1 GUI with 3 buttons?

That way, you can show or hide the buttons, rather than deleting GUI's.

Just a thought.


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
tonycst

Still main GUI will not start after i close settings dialog.

It will if i call function with perhaps a traymenuitem but i want it to be called when i close Settings GUI.

When i close Settings GUI, it does RETURN and then nothing happens.

I needed 3 gui's for:

So i can move then around or have one or all 3.

I cant find a solution to display just the icon on the screen, so i had to create a GUI+Button and cover button with an icon because transparent GUI seems impossible.

Share this post


Link to post
Share on other sites
ripdad

It ain't impossible until all has been exhausted. And even then, something still might be overlooked.

It's not perfect -- but this should get you going...

#RequireAdmin
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=MonOff.ico
#AutoIt3Wrapper_Outfile=..\Laptop Monitor OFF.exe
#AutoIt3Wrapper_Compression=0
#AutoIt3Wrapper_UseUpx=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>

FileInstall("MonOff.ico", @TempDir & "\MonOff.ico", 1)
FileInstall("Restart.ico", @TempDir & "\Restart.ico", 1)
FileInstall("ShutDown.ico", @TempDir & "\ShutDown.ico", 1)

Opt('WinTitleMatchMode', 4)
Opt('TrayOnEventMode', 1 + 2)
Opt('TrayAutoPause', 0)

Global Const $IniFile = @ScriptDir & '\Settings.ini'
Global Const $TRAY_UNCHECKED = 4, $TRAY_DEFAULT = 512
Global Const $HWND = WinGetHandle('classname=Progman')
Global Const $SC_MonitorPower = 61808
Global Const $WM_SYSCommand1 = 274
Global Const $Power_On = -1
Global Const $Power_Off = 2
Global Const $NA = -100000

Global $GUI, $OFF = $NA, $Restart = $NA, $Shutdown = $NA

Global $Tray = TrayCreateItem('Settings')
TrayItemSetState($Tray, $TRAY_DEFAULT)
TrayItemSetOnEvent($Tray, '_Settings')
TraySetState()

If Not FileExists($IniFile) Or IniRead(@ScriptDir & '\Settings.ini', 'ButtonPosition', 'X', '') = '' Then
    _IniInit()
EndIf
_MAIN()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $OFF
            MsgBox(0, '', '$SC_MonitorPower')
            ;DllCall("user32.dll", "int", "SendMessage", "hwnd", $HWND, "int", $WM_SYSCommand1, "int", $SC_MonitorPower, "int", $Power_Off)
        Case $Restart
            MsgBox(0, '', 'Shutdown(2)')
            ;Shutdown(2)
        Case $Shutdown
            MsgBox(0, '', 'Shutdown(8)')
            ;Shutdown(8)
    EndSwitch
WEnd

Func _MAIN()
    Local $str1 = IniRead($IniFile, 'Display_Elements', 'MonOff', 0)
    Local $str2 = IniRead($IniFile, 'Display_Elements', 'Restart', 0)
    Local $str3 = IniRead($IniFile, 'Display_Elements', 'ShutDown', 0)
    ;==============================================================================
    If ($str1 = 0) And ($str2 = 0) And ($str3 = 0) Then
        MsgBox(16, 'Error', 'Nothing to show. Please access tray menu to set which buttons to show and click "Save"')
        Return
    EndIf
    ;==============================================================================
    Local $MainX = IniRead($IniFile, 'ButtonPosition', 'X', 0)
    Local $MainY = IniRead($IniFile, 'ButtonPosition', 'Y', 0)
    Local $iOffset = 0
    ;==============================================================================
    $GUI = GUICreate('My_Buttons', 0, 0, $MainX, $MainY, $WS_POPUP, $WS_EX_TOPMOST, WinGetHandle(AutoItWinGetTitle()))
    ;==============================================================================
    If $str1 = 1 Then
        $OFF = GUICtrlCreateButton('OFF', 0, $iOffset, 128, 128, $BS_ICON)
        GUICtrlSetImage(-1, @TempDir & '\MonOff.ico')
        WinMove('My_Buttons', '', $MainX, $MainY, 128, $iOffset + 128)
        $iOffset += 128
    Else
        $OFF = $NA
    EndIf
    ;==============================================================================
    If $str2 = 1 Then
        $Restart = GUICtrlCreateButton('Restart', 0, $iOffset, 128, 128, $BS_ICON)
        GUICtrlSetImage(-1, @TempDir & '\Restart.ico')
        WinMove('My_Buttons', '', $MainX, $MainY, 128, $iOffset + 128)
        $iOffset += 128
    Else
        $Restart = $NA
    EndIf
    ;==============================================================================
    If $str3 = 1 Then
        $Shutdown = GUICtrlCreateButton('Shutdown', 0, $iOffset, 128, 128, $BS_ICON)
        GUICtrlSetImage(-1, @TempDir & '\ShutDown.ico')
        WinMove('My_Buttons', '', $MainX, $MainY, 128, $iOffset + 128)
    Else
        $Shutdown = $NA
    EndIf
    ;==============================================================================
    WinSetTrans($GUI, '', IniRead(@ScriptDir & '\Settings.ini', 'ButtonSettings', 'Visibility', '255'))
    GUISetState(@SW_SHOW, $GUI)
EndFunc

Func _Settings()
    GUIDelete($GUI)
    TrayItemSetState($Tray, $TRAY_DEFAULT + $TRAY_UNCHECKED)
    TrayItemSetOnEvent($Tray, '')
    ;==============================================================================
    Local $SetingsGUI = GUICreate("Settings", 215, 215)
    GUICtrlCreateGroup("Coordinates", 5, 5, 205, 60)
    GUICtrlCreateLabel("How many pixels from the LEFT", 10, 23, 150, 18)
    Local $Xinput = GUICtrlCreateInput(IniRead($IniFile, "ButtonPosition", "X", 0), 168, 22, 35, 18, $ES_NUMBER)
    GUICtrlCreateLabel("How many pixels from the TOP", 10, 43, 150, 18)
    Local $Yinput = GUICtrlCreateInput(IniRead($IniFile, "ButtonPosition", "Y", 0), 168, 42, 35, 18, $ES_NUMBER)
    ;==============================================================================
    GUICtrlCreateGroup("Transparency", 5, 65, 205, 40)
    GUICtrlCreateLabel("Set visibility from 0-255", 10, 82, 150, 18, $ES_CENTER)
    Local $nVis = IniRead($IniFile, "ButtonSettings", "Visibility", '')
    If Not $nVis Then
        $nVis = 255
        IniWrite($IniFile, "ButtonSettings", "Visibility", $nVis)
    EndIf
    Local $Visibility = GUICtrlCreateInput($nVis, 168, 80, 30, 20, $ES_NUMBER)
    ;==============================================================================
    GUICtrlCreateGroup("Select Buttons to show", 5, 105, 205, 40)
    Local $EnableGUIMonOff = GUICtrlCreateCheckbox("Screen Off", 10, 120)
    If IniRead($IniFile, "Display_Elements", "MonOff", '') = 1 Then
        GUICtrlSetState($EnableGUIMonOff, $GUI_Checked)
    Else
        GUICtrlSetState($EnableGUIMonOff, $GUI_UnChecked)
    EndIf

    Local $EnableGUIRestart = GUICtrlCreateCheckbox("Reboot", 85, 120)
    If IniRead($IniFile, "Display_Elements", "Restart", '') = 1 Then
        GUICtrlSetState($EnableGUIRestart, $GUI_Checked)
    Else
        GUICtrlSetState($EnableGUIRestart, $GUI_UnChecked)
    EndIf

    Local $EnableGUIShutDown = GUICtrlCreateCheckbox("Turn Off", 145, 120)
    If IniRead($IniFile, "Display_Elements", "ShutDown", '') = 1 Then
        GUICtrlSetState($EnableGUIShutDown, $GUI_Checked)
    Else
        GUICtrlSetState($EnableGUIShutDown, $GUI_UnChecked)
    EndIf
    ;==============================================================================
    Local $EnableStartUp = GUICtrlCreateButton("Enable Start with Windows", 8, 150, 200, 25)
    RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", "MonitorOff")
    If @error Then
        GUICtrlSetData($EnableStartUp, "Enable Start with Windows")
    Else
        GUICtrlSetData($EnableStartUp, "Disable Start with Windows")
    EndIf
    ;==============================================================================
    Local $SaveAndTry = GUICtrlCreateButton("Save and Try", 8, 180, 100, 25)
    Local $Finish = GUICtrlCreateButton("Finish", 108, 180, 100, 25)
    ;==============================================================================
    GUISetState(@SW_SHOW, $SetingsGUI)

    While 1
        Switch GUIGetMsg()
            Case $Finish, $GUI_EVENT_CLOSE
                ExitLoop
            Case $SaveAndTry
                IniWrite($IniFile, "ButtonPosition", "X", GUICtrlRead($Xinput))
                IniWrite($IniFile, "ButtonPosition", "Y", GUICtrlRead($Yinput))
                IniWrite($IniFile, "ButtonSettings", "Visibility", GUICtrlRead($Visibility))
                IniWrite($IniFile, "Display_Elements", "MonOff", Number(GUICtrlRead($EnableGUIMonOff) = $GUI_Checked))
                IniWrite($IniFile, "Display_Elements", "Restart", Number(GUICtrlRead($EnableGUIRestart) = $GUI_Checked))
                IniWrite($IniFile, "Display_Elements", "ShutDown", Number(GUICtrlRead($EnableGUIShutDown) = $GUI_Checked))
                _MAIN()
                MsgBox(8256, "Preview", "Press OK when done")
                GUIDelete($GUI)
            Case $EnableStartUp
                If GUICtrlRead($EnableStartUp) = "Disable Start with Windows" Then
                    RegDelete("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", "MonitorOff")
                    GUICtrlSetData($EnableStartUp, "Enable Start with Windows")
                Else
                    RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run", "MonitorOff", "REG_SZ", @ScriptFullPath)
                    GUICtrlSetData($EnableStartUp, "Disable Start with Windows")
                EndIf
        EndSwitch
    WEnd
    GUIDelete($SetingsGUI)
    TrayItemSetOnEvent($Tray, '_Settings')
    _MAIN()
EndFunc
;
Func _IniInit()
    IniWrite($IniFile, "ButtonPosition", "X", 0)
    IniWrite($IniFile, "ButtonPosition", "Y", 0)
    IniWrite($IniFile, "Display_Elements", "MonOff", 1)
    IniWrite($IniFile, "Display_Elements", "Restart", 1)
    IniWrite($IniFile, "Display_Elements", "ShutDown", 1)
    IniWrite($IniFile, "ButtonSettings", "Visibility", 255)
EndFunc
;

By the way --

It's a good idea to start your own thread, rather than piggy-back on some elses.

Good luck!


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
tonycst

i dont know what piggy-back means but OK.

Thanks

Share this post


Link to post
Share on other sites
water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
tonycst

Didnt feel like posting another topic. Not because am lazy, but because i dont feel like starting whole topic for what is relatively close discussed here.

Major reason why i dont like to start topics is because.

3 people have exact same problem. They all start a new topic but word their issue 3 different ways.

Forum gets full of same topics title differently, yet the solution for all 3 is the same. Thats why so many moderators reply with "discussed here" links instead of giving an answer.

I feel bad receiving "its answered here" or "similar post i found here" and such, so i'd rather not start a new one that would be answered with a link to another topic.

For 1: reference topic can be removed or link broken, tus making my topic unanswered and forgotten, just like they are (some of my recent once anyway)

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  

×