Jump to content

Merging GUI loop with main program loop???


Go to solution Solved by brokenbeforetime,

Recommended Posts

Hi I'm new to Autoit and the fourm. I have made a script/program that turns the monitor off wen the PC has been idle for a given time and the CPU usage is also under a given percent.  It works fine but I am using two scripts, one for a system tray icon with menus to change settings ect. and one to monitor idle time/CPU usage and turn the monitor off ect.  So basically I have two loops running at one time, one for the GUI and one for the program's function. Is it possible to combine the two into one script/loop?... Here is an example of what I tried...

func guiloop()

        Switch TrayGetMsg()
            Case $Exit

               EXIT

         EndSwitch

endfunc

Do
      $iIdleTime = _Timer_GetIdleTime()
      sleep (250)

      call("guiloop")
Until  $iIdleTime > $sMinutes

This almost seem to work, the loop runs and the menus seem to function except the code for the case doesn't run. when I access the menu the loop pauses and continues after I click a menu item, but the code attached to the item doesn't run. I hope I'm explaining myself well enough, I realize this is just a simple example but my full script is over 1500 lines and like I said I'm pretty new to Autoit so its kind of a mess right now.

Any advice will be GREATLY appreciated, even if you just let me know if this would be possible or have any ideas at all... I really want to get this working as one script. Thanks.

Edited by brokenbeforetime
Link to comment
Share on other sites

#NoTrayIcon
#include <MsgBoxConstants.au3>
#include <TrayConstants.au3> ; Required for the $TRAY_ICONSTATE_SHOW constant.

Opt("TrayMenuMode", 3) ; The default tray menu items will not be shown and items are not checked when selected. These are options 1 and 2 for TrayMenuMode.

Example()

Func Example()
    Local $iSettings = TrayCreateMenu("Settings") ; Create a tray menu sub menu with two sub items.
    Local $iDisplay = TrayCreateItem("Display", $iSettings)
    Local $iPrinter = TrayCreateItem("Printer", $iSettings)
    TrayCreateItem("") ; Create a separator line.

    Local $idAbout = TrayCreateItem("About")
    TrayCreateItem("") ; Create a separator line.

    Local $idExit = TrayCreateItem("Exit")

    TraySetState($TRAY_ICONSTATE_SHOW) ; Show the tray menu.

    While 1
        Switch TrayGetMsg()
            Case $idAbout ; Display a message box about the AutoIt version and installation path of the AutoIt executable.
                MsgBox($MB_SYSTEMMODAL, "", "AutoIt tray menu example." & @CRLF & @CRLF & _
                        "Version: " & @AutoItVersion & @CRLF & _
                        "Install Path: " & StringLeft(@AutoItExe, StringInStr(@AutoItExe, "\", 0, -1) - 1)) ; Find the folder of a full path.

            Case $iDisplay, $iPrinter
                while 1
                    MsgBox(0,'this is my monitor loop','')
                WEnd

            Case $idExit ; Exit the loop.
                ExitLoop
        EndSwitch
    WEnd
EndFunc   ;==>Example

99% of this is taken from the help file that comes with autoit.  If you click on 'settings - display or print' it will run a loop.

Link to comment
Share on other sites

That doesn't solve my problem. The monitor loop should always be running and i should be able to use the menu buttons while it is running.  I know its hard to explain... I don't even know if it possible.  I want the case loop to be within another loop that is constantly performing its action. So in the example you posted, I want the msg box to keep looping but when I click exit the program should exit.

I am using two scripts, one for a system tray icon and menus and one to monitor idle time/CPU usage. I want to combine that into one script.

Link to comment
Share on other sites

From the example above.... If you run the following code you will notice it beeps every 3 seconds, but the menus items (cases) no longer work when you click them...is it possible to have the following script beep every 3 seconds and the menu items still function....thats what I'm trying to do...

#NoTrayIcon
#include <MsgBoxConstants.au3>
#include <TrayConstants.au3> ; Required for the $TRAY_ICONSTATE_SHOW constant.

Opt("TrayMenuMode", 3) ; The default tray menu items will not be shown and items are not checked when selected. These are options 1 and 2 for TrayMenuMode.

Example()

Func Example()
    Local $iSettings = TrayCreateMenu("Settings") ; Create a tray menu sub menu with two sub items.
    Local $iDisplay = TrayCreateItem("Display", $iSettings)
    Local $iPrinter = TrayCreateItem("Printer", $iSettings)
    TrayCreateItem("") ; Create a separator line.

    Local $idAbout = TrayCreateItem("About")
    TrayCreateItem("") ; Create a separator line.

    Local $idExit = TrayCreateItem("Exit")

    TraySetState($TRAY_ICONSTATE_SHOW) ; Show the tray menu.

    While 1
        Switch TrayGetMsg()
            Case $idAbout ; Display a message box about the AutoIt version and installation path of the AutoIt executable.
                MsgBox($MB_SYSTEMMODAL, "", "AutoIt tray menu example." & @CRLF & @CRLF & _
                        "Version: " & @AutoItVersion & @CRLF & _
                        "Install Path: " & StringLeft(@AutoItExe, StringInStr(@AutoItExe, "\", 0, -1) - 1)) ; Find the folder of a full path.

            Case $iDisplay, $iPrinter
                while 1
                    MsgBox(0,'this is my monitor loop','')
                WEnd

            Case $idExit ; Exit the loop.
                ExitLoop
        EndSwitch
   
    Beep(500, 50)
    sleep(3000)


  WEnd
EndFunc   ;==>Example
Edited by brokenbeforetime
Link to comment
Share on other sites

  • Solution

Think I found a better way using TraySetOnEvent, main loop runs while still allowing tray items to function. If you need to do the same thing with a form and buttons instead of system tray, use GUICtrlSetOnEvent.

Basicaly this lets you create GUI without useing While 1/Switch/case...

Heres an example useing TraySetOnEvent:

#include <GUIConstantsEx.au3>

Opt("TrayOnEventMode", 1) ; Enable TrayOnEventMode
Opt("TrayMenuMode", 3) ; The default tray menu items will not be shown and items are not checked when selected. These are options 1 and 2 for TrayMenuMode.

Local $idExit = TrayCreateItem("Exit") ;creates tray item
TrayItemSetOnEvent($idExit, "EXITButton") ;links tray item to func EXITButton

While 1
    Beep(500, 100) ; Main code/loop here, runs continually
    Sleep(2000)
WEnd

func EXITButton() ;this function runs when tray item is clicked
    Exit
EndFunc
Edited by brokenbeforetime
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...