Jump to content

A few questions about "While 1" and SystemTray problem


rafalp
 Share

Recommended Posts

Hi everyone.

I have few question about code in AutoIT. I would be grateful if somebody could anwser it.

1. I got loop and code like in documetation:

Local $aboutitem = TrayCreateItem("About")
TrayCreateItem("")
Local $exititem = TrayCreateItem("Exit")
While 1

;---------------------------------------------------------------- MENU
Local $msg = TrayGetMsg()
Select
Case $msg = 0
ContinueLoop
Case $msg = $aboutitem
MsgBox(64, "About:", "AutoIt3-Tray-sample")
Case $msg = $exititem
Exit;
EndSelect
;---------------------------------------------------------------- END MENU


; my long long code
; my long long code
; my long long code
; my long long code

Sleep(250)

WEnd

- Exit not working ;)

Maybe "; my long long code " should not be in that place?

2. BTW "While 1", is it good method to write program like that? Program is sitting in tray and only do some calculation with mouse position. Do this not overload processor? If I make longer Sleep() for example 1000 it would less use computer processor?

3. Is there method to use in tray both Opt("TrayMenuMode") checked and unchecked? (some Item I want to be like "checkbox" [ON/OFF] and some normal like "Exit", "About")

Thanks in advance.

PS. AutoIT is so awesome! Thank U Developers! :)

Link to comment
Share on other sites

  • Moderators

rafalp,

Welcome to the AutoIt forum. :)

In reverse order:

- 3. As far as I know you can only have "all ticks" or "no ticks" in a menu via Opt("TrayMenuMode") - if you want the tick not to appear for certain items then you have to uncheck it programatically.

- 2. If you have GUIGetMsg or TrayGetMsg in your loop then there is no need for a Sleep - these functions idle the CPU automatically. If you do not have a loop then you need a sleep, but Sleep(10) is perfectly adequate.

- 1. The problem with your "long long code" is that it blocks the script until it has ended - that is why you think the "Exit" does not work. There are 2 solutions: run through the TrayMsg queue until you have used everything in the queue - or use TrayOnEvent mode so that the tray interrupts the code.

Now here are 2 scripts showing all of the above: ;)

Firstly a script using TrayGetMsg:

#include <GUIConstantsEx.au3>

Opt("TrayMenuMode", 1) ; Default tray menu items will not be shown

Local $aboutitem = TrayCreateItem("About")
TrayCreateItem("")
Local $exititem = TrayCreateItem("Exit")
ConsoleWrite("Exit Message = " & $exititem & @CRLF)

While 1

    ; loop until all TrayMsg queue is acted on
    While 1
        Local $msg = TrayGetMsg()
        ConsoleWrite("Tray Message: " & $msg & @CRLF)
        Select
            ; Exit this loop when TrayMsg empty
            Case $msg = 0
                ExitLoop
            Case $msg = $aboutitem
                TrayItemSetState($aboutitem, $GUI_UNCHECKED) ; Force unchecked
                MsgBox(64, "About:", "AutoIt3-Tray-sample")
            Case $msg = $exititem
                Exit
        EndSelect
    WEnd

    ; Simulate long code
    Sleep(5000)

    ; No need for this as TrayGetMsg (and GUIGetMsg) do an automatic idle
    ;Sleep(250)

WEnd

See how you get lots of negative TrayMessages? These are the mouse actions to open the menu and click on it. If you only read one message at a time imagine how long it takes you to get to $exititem if each loop runs your "long code! :)

Now one using TrayOnEvent mode:

#include <GUIConstantsEx.au3>

Opt("TrayOnEventMode", 1)
Opt("TrayMenuMode", 1) ; Default tray menu items will not be shown

Local $aboutitem = TrayCreateItem("About")
TrayItemSetOnEvent(-1, "_On_About")
TrayCreateItem("")
Local $exititem = TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "_On_Exit")

While 1
    ; Simulate long code
    Sleep(5000)
WEnd

Func _On_About()
    TrayItemSetState($aboutitem, $GUI_UNCHECKED) ; Force unchecked
    MsgBox(64, "About:", "AutoIt3-Tray-sample")
EndFunc

Func _On_Exit()
    Exit
EndFunc

See how it reacts immediately? No need to wait for the "long code" to finish. ;)

All clear? Please ask if not. :D

M23

Edit: I almost forgot. As you did not use Opt("TrayMenuMode", 1), you still had the default menu items in your traymenu - this meant that you paused the script every time you clicked on the icon, so it is little wonder you never got the "Exit" to work! ;)

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thank you @Melba23 ;) Works nice:D

I will be using "TrayOnEvent mode" because I think it is better code separeted.

Going on.. I came to GUI creation and I have this same problem :) but with trigger GIU events:

My all template-code (nothing to analize)

#include <guiconstantsex.au3>

Opt("TrayOnEventMode", 1)
Opt("TrayMenuMode", 1) ; Default tray menu items will not be shown

Local $aboutitem = TrayCreateItem("About")
TrayItemSetOnEvent(-1, "_On_About")
TrayCreateItem("")
Local $exititem = TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "_On_Exit")
TrayCreateItem("")
Local $settingsitem = TrayCreateItem("Settings")
TrayItemSetOnEvent(-1, "GetGUI") ; --------------------- GUI reference (it is in include giu.au3)


While 1
; my long long code
; my long long code
; my long long code
; my long long code
WEnd

; ----- GUI -------------
#include "gui.au3"
; ----- GUI -------------


; ----------------------------------- TRAY FUNCTION

Func _On_About()
TrayItemSetState($aboutitem, $GUI_UNCHECKED) ; Force unchecked
MsgBox(64, "About:", "AutoIt3-Tray-sample")
EndFunc

Func _On_Exit()
Exit
EndFunc

- but, and now GIU.au3:

#include <buttonconstants.au3>
#include <editconstants.au3>
#include <staticconstants.au3>
#include <windowsconstants.au3>
Opt("GUIOnEventMode", 1)


Func GetGUI()

#Region ### START Koda GUI section ###
; KODA Forms code here
#EndRegion ### END Koda GUI section ###

; EVENTS
GUISetOnEvent($GUI_EVENT_CLOSE, "CloseGIU")

While 1
Sleep(100)
WEnd

EndFunc


Func CloseGIU() ; ------------- NOT WORKING :/
GUIDelete()
EndFunc

Edit: I almost forgot. As you did not use Opt("TrayMenuMode", 1), you still had the default menu items in your traymenu - this meant that you paused the script every time you clicked on the icon, so it is little wonder you never got the "Exit" to work!

It's OK, I take care with TrayMenuMode so it wasn't problem. The problem was like you explain before :D

PS. how to write code with TAB in /autoit/ CODE //autoit/ ? Copy/Pase delete my TAB.</windowsconstants.au3></staticconstants.au3></editconstants.au3></buttonconstants.au3></guiconstantsex.au3>

Edited by rafalp
Link to comment
Share on other sites

  • Moderators

rafalp,

You have not included GUIConstantsEx.au3 so the script will not recognise $GUI_EVENT_CLOSE and you have not specified a GUI handle in GUIDelete(). If I correct those 2 errors the script works fine. :)

Might I suggest doenloading and installing the full version of SciTE4AutoIt3 from here. It gives you losts of extra goodies to help you code in AutoIt one of which is Au3Check which catches errors like that for you. :D

I am afraid I cannot understand your P.S. - it has become corrupted. Please ask the question again. ;)

M23

Edited by Melba23
Fixed tags

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Melba, I include <GUIConstantsEx.au3> in first code (before including GUI stuff)

So I add GUI created handle:

$Form1_1 = GUICreate("Form1", 423, 400, 279, 192)

GUISetOnEvent($GUI_EVENT_CLOSE, "CloseGIU", $Form1_1)

and change exit function:

Func CloseGIU($handle)

GUIDelete($handle)

EndFunc

But nothing again:/

PS., I mean that I can't include in forum posts auto it script:

[tab]code ; no TAB (?)
[tab]code ; no TAB (?)

with tabulator key for more human readable code.

(When I COPY/PASE to post and publish - TABs are destroyed)

Edited by rafalp
Link to comment
Share on other sites

  • Moderators

rafalp,

Look at the syntax for GUISetOnEvent and GUIDelete in the Help file. ;)

This works for me: :)

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

Global $hGUI

GetGUI()

Func GetGUI()

    $hGUI = GUICreate("Form1", 423, 400, 279, 192) ; $hGUI is the GUI handle
    GUISetState()

    ; EVENTS
    GUISetOnEvent($GUI_EVENT_CLOSE, "CloseGUI") ; No handle needed here

    While 1
        Sleep(100)
    WEnd

EndFunc   ;==>GetGUI

Func CloseGUI() ; ------------- NOT WORKING :/
    GUIDelete($hGUI) ; But we need the handle here
EndFunc   ;==>CloseGUI

If you want to post code and keep the formatting use the basic editor mode (click on the button at top left of the editor itself to change modes) and then use Code tags - put [autoit] before and [/autoit] after your posted code. :D

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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