Jump to content

Script pauses when I click tray icon


Floppy
 Share

Recommended Posts

Hello, please can you help me with this script?

Why when I click the tray icon, the script pauses?

#include <GUIConstants.au3>
#include <Sound.au3>
 
Opt("TrayMenuMode", 3)
Opt("TrayAutoPause", 0)
 
$acc_job_sound = @DesktopDir&"\siren.mp3"
Global $t_ricev
Global $t_end
Global $job_sound
$job_sound = _SoundOpen($acc_job_sound)
 
TrayCreateItem("")
$t_chiudi = TrayCreateItem("Chiudi")
Start_alarm()
While 1
$tsg = TrayGetMsg()
 
Select
 
  Case $tsg = $t_ricev
   Stop_alarm()
  
  Case $tsg = $t_end
   Stop_alarm()
   TrayItemDelete($t_end)
   Controlla()
  
EndSelect
 
WEnd
Func Stop_alarm()
 
AdlibUnRegister("Play_sound")
_SoundClose($job_sound)
TrayItemDelete($t_ricev)
 
EndFunc
Func Start_alarm()
 
$t_ricev = TrayCreateItem("Opz1", Default, 0)
$t_end = TrayCreateItem("Opz2", Default, 1)
 
AdlibRegister("Play_sound", _SoundLength($job_sound, 2))
 
EndFunc
Func Play_sound()
 
_SoundPlay($job_sound)
 
EndFunc
Link to comment
Share on other sites

  • Moderators

FSoft,

The tray menu is modal (like all menus) - that means it blocks execution of the script until the menu is closed. So your entire script (including the Adlib function) ceases until you close the menu. This is a Windows "feature" and not a bug in Autoit - the same thing happens when you move a window - the script running within it is paused until the window is released. :oops:

However, you can get round this problem by using a Timer like this:

#include <GUIConstantsEx.au3> ; GUIConstants.au3 is deprecated <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#include <Sound.au3>
#Include <Timers.au3> ; New include file <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Opt("TrayMenuMode", 3)
;Opt("TrayAutoPause", 0) ; Not needed - read the Help file carefully <<<<<<<<<<<<<<<<<<<<<<<<<

$acc_job_sound = "M:\Downloads\Test.mp3"
Global $t_ricev
Global $t_end
Global $iTimer ; Add this <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Global $job_sound = _SoundOpen($acc_job_sound)

TrayCreateItem("")
$t_chiudi = TrayCreateItem("Chiudi")

$hGUI = GUICreate("Test", 1, 1)
GUISetState(@SW_HIDE)

Start_alarm()

While 1

    Switch TrayGetMsg()
        Case $t_chiudi
            _Timer_KillAllTimers($hGUI) ; Do not forget to kill all timers on exit <<<<<<<<<<<<
            Exit

        Case $t_ricev
            Stop_alarm()

        Case $t_end
            Stop_alarm()
            TrayItemDelete($t_end)
            ;Controlla()

    EndSwitch

WEnd

Func Stop_alarm()

    _Timer_KillTimer($hGUI, $iTimer) ; Kill the timer <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    ;AdlibUnRegister("Play_sound")

    _SoundClose($job_sound)
    TrayItemDelete($t_ricev)

EndFunc   ;==>Stop_alarm

Func Start_alarm()

    $t_ricev = TrayCreateItem("Opz1", Default, 0)
    $t_end = TrayCreateItem("Opz2", Default, 1)

    $iTimer = _Timer_SetTimer($hGUI, _SoundLength($job_sound, 2), "Play_sound") ; Start a timer <<<<<<<<<<<<<<<<
    ;AdlibRegister("Play_sound", _SoundLength($job_sound, 2))

    _SoundPlay($job_sound)

EndFunc   ;==>Start_alarm

Func Play_sound($hWnd, $Msg, $iIDTimer, $dwTime) ; Yes you do need all these parameters! <<<<<<<

    #forceref $hWnd, $Msg, $iIDTimer, $dwTime

    _SoundStop($job_sound) ; Stop the sound before replaying it <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    _SoundPlay($job_sound)

EndFunc   ;==>Play_sound

I think the comments are clear enough - please ask if not. :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

Thank you Melba. Please can you help me with the same thing in the following script?

#include <GUIConstantsEx.au3>
#Include <IE.au3>
#Include <String.au3>
#include <Array.au3>
#include <INet.au3>
#include <Date.au3>
#Include <Timers.au3>
Opt("TrayMenuMode", 3)

$intervallo = 1000

$m_gui = GUICreate("Test", 800, 600)
$IE = ObjCreate("Shell.Explorer.2")
GUICtrlCreateObj($IE, 0, 0, 800, 600)
GUISetState(@SW_SHOW)

TrayCreateItem("")
$t_chiudi = TrayCreateItem("Chiudi")
Controlla()
While 1

$msg = GUIGetMsg()
$tsg = TrayGetMsg()

Select
 
  Case $msg = $GUI_EVENT_CLOSE Or $tsg = $t_chiudi
    _Timer_KillAllTimers($m_gui)
   Exit
  
EndSelect

WEnd
Func Controlla()
 
  Do
   Global $login_src = _INetGetSource("http://google.com")
  
   $jobs_found = StringInStr($login_src, '<ol>')
   Sleep($intervallo)
  
  Until $jobs_found <> 0
  
  Cerca()
 
EndFunc

Func Cerca()

MsgBox(0,'','ciao')
EndFunc

I tried to implement a timer here too, but, for a strange reason, seems like the script freezes at a point (the window title becomes "Test - Not responding").

Link to comment
Share on other sites

  • Moderators

FSoft,

Where is the Timer - and why do you need one? :D

Furthermore there is no way you ever get to poll Tray or GUI events as you seem to be locked inside the Controlla function. What exactly are you trying to do here? :oops:

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

This is a piece of a script of mine.

The function Controlla() just search the source of a page ($login_src) for a string ($jobs_found). When that string is found another function is called (Cerca()).

The problem is that when the Do loop inside Controlla() function is running every 1 second, I can't close the program because the program is freezed.

I hope now everything is clear...

Link to comment
Share on other sites

  • Moderators

FSoft,

Just look for GUI and Tray events within the function instead of Sleeping - look for the <<<<<<< lines: :oops:

#include <GUIConstantsEx.au3>
#include <IE.au3>
#include <String.au3>
#include <Array.au3>
#include <INet.au3>
#include <Date.au3>
 
Opt("TrayMenuMode", 3)
 
$intervallo = 1000
 
$m_gui = GUICreate("Test", 800, 600)
$IE = ObjCreate("Shell.Explorer.2")
GUICtrlCreateObj($IE, 0, 0, 800, 600)
GUISetState(@SW_SHOW)
 
TrayCreateItem("")
$t_chiudi = TrayCreateItem("Chiudi")
 
Controlla()
 
While 1
 
    $msg = GUIGetMsg()
    $tsg = TrayGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE Or $tsg = $t_chiudi
            Exit
    EndSelect
 
WEnd
Func Controlla()
 
    Do
        Global $login_src = _INetGetSource("http://google.com")
 
        $jobs_found = StringInStr($login_src, '<ol>')
 
        ; Here we wait for the delay time  ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        $iBegin = TimerInit()
        While TimerDiff($iBegin) < $intervallo
 
            ; But do things while we wait
            $msg = GUIGetMsg()
            $tsg = TrayGetMsg()
            Select
                Case $msg = $GUI_EVENT_CLOSE Or $tsg = $t_chiudi
                    Exit
            EndSelect
        WEnd  ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
    Until $jobs_found <> 0
 
    Cerca()
 
EndFunc   ;==>Controlla
 
Func Cerca()
    MsgBox(0, '', 'ciao')
EndFunc   ;==>Cerca

No timers needed now. All clear? :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

  • Moderators

FSoft,

it doesn't work except if you don't click Close every 5 seconds

What does not work? You only need to click "Close" if you want to exit! :D

And there is no reason at all why the length of the delay should have any effect at all on the script other than setting the interval between reading the source from the URL. :oops:

Are you trying to say that the script pauses if you open the tray menu? If so then I have already shown you how to get over that problem by using a Timer. If not than please explain a little more clearly. :rip:

I am very willing to help but I need to understand the problem you are having. :)

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

Thank you Melba. LOL I just made a mistake copying/pasting your version of the script. LOL

Now I have another problem. I'm trying to close the program only when the Close button of a window is pressed.

I made this, but it doesn't work.

#include <GUIConstantsEx.au3>

Opt("TrayMenuMode", 3)

$m_gui = GUICreate("Test", 800, 600)
GUISetState(@SW_SHOW, $m_gui)
$q_gui = GUICreate("Prova", 800, 600)
GUISetState(@SW_SHOW, $q_gui)

While 1

    $msg = GUIGetMsg(1)
    Select
  Case $msg = $GUI_EVENT_CLOSE
   MsgBox(0,'',$msg[1])
            Exit
    EndSelect

WEnd
Link to comment
Share on other sites

GUIGetMsg ( [advanced] )

advanced [optional] return extended information in an array.

0 = (default) Returns a single event.

1 = returns an array containing the event and extended information.

Your case statement is incorrect, if you're using the advanced parameter, you need to refer to the $msg as an array, not as a variable. Edited by BrewManNH

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

Link to comment
Share on other sites

I tried this but it doesn't work again:

#include <GUIConstantsEx.au3>
Global $about_gui

Opt("TrayMenuMode", 3)

$m_gui = GUICreate("Test", 800, 600)
GUISetState(@SW_SHOW, $m_gui)
$q_gui = GUICreate("Prova", 800, 600)
GUISetState(@SW_SHOW, $q_gui)

While 1

    $msg = GUIGetMsg(1)
   Switch $msg[1]
    
  Case $m_gui
     
   Select
      
    Case $msg = $GUI_EVENT_CLOSE
     Exit
      
   EndSelect
     
  Case $about_gui
     
   Select
     
    Case $msg = $GUI_EVENT_CLOSE
     GUIDelete($about_gui)
      
   EndSelect
     
    
EndSwitch

WEnd
Link to comment
Share on other sites

You create 2 GUIs, $m_gui and $q_gui, there's no $about_gui in your script.

Then you're still not using the GUIGetMsg return array correctly in your checks for messages. See if this doesn't work better for you than how you wrote it.

#include <guiconstantsex.au3>
Global $about_gui=9999

Opt("TrayMenuMode", 3)

$m_gui = GUICreate("Test", 800, 600)
GUISetState(@SW_SHOW, $m_gui)
$q_gui = GUICreate("Prova", 800, 600)
GUISetState(@SW_SHOW, $q_gui)

While 1
    $msg = GUIGetMsg(1)
    Switch $msg[1]
        Case $m_gui
            Select
                Case $msg[0] = $GUI_EVENT_CLOSE ; <<<<<<< $msg is still an array, check it correctly
                    Exit
            EndSelect
        Case $q_gui ; <<<<<<<< you don't create a gui and assign it to a variable named $about_gui, check the correct variables
            Select
                Case $msg[0] = $GUI_EVENT_CLOSE
                    GUIDelete($q_gui) ; <<<<<<<<< close the correct window
            EndSelect
    EndSwitch
WEnd
Edited by BrewManNH

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

Link to comment
Share on other sites

It's showing the loading cursor because it's loading the web site in the Controlla function (Global $login_src = _INetGetSource(http://google.com)) inside the Do loop in that function. The only way to avoid that is to not try to load a web page every time through the Do loop I would assume.

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

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