Jump to content
Sign in to follow this  

ProcessExists to modify $TRAY_EN/DISABLE

Recommended Posts



Been a while since I was last active but here I am again...

This simplified tray menu allows you to launch an instance of VLC player, with a second tray option to "Close VLC".

The issue is, that I'm using the '--one-instance' option with VLC and this means though the player is persistant, the PID changes.

When you click "VLC" to launch it, the tray menu to "Close VLC" is enabled.

When you click "Close VLC", it closes the instance of VLC based on the PID of the launched instance.

Now, the problem:

When you click "VLC" to launch it, and then click "VLC" again, the PID changes and the "Close VLC" tray menu becomes disabled. So you're unable to close the 'new' instance...

It's probably something extremely obvious, but I've played with it for some time and am unable to get the "Close VLC" to correctly behave according to the existence of a launched VLC, regardless that the PID changes.

I'm aiming to keep it PID based as I want to control the VLC application launched by this tray menu exclusively, regardless if there is another separately launched instance.

Hope someone can assist...

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <Constants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>
Global $pid
Opt("TrayMenuMode", 1)

$item_menu = TrayCreateMenu("Menu")
$item_vlc = TrayCreateItem("VLC", $item_menu)
$item_close = TrayCreateItem("Closer VLC")
$item_exit = TrayCreateItem("Exit")


While 1
    $msg = TrayGetMsg()
    If ProcessExists($pid) = 0 Then TrayItemSetState($item_close, $TRAY_DISABLE)
        Case $msg = 0
        Case $msg = $item_vlc
            $pid = PlayIt()
            TrayTip("Debug", "PID is " & $pid, 5)
        Case $msg = $item_close
            TrayItemSetState($item_close, $TRAY_DISABLE)
        Case $msg = $item_exit


Func PlayIt()
    $pid = Run(@ProgramFilesDir & "\VideoLAN\VLC\vlc.exe --one-instance")

    TrayItemSetState($item_close, $TRAY_ENABLE)
    Return $pid

Share this post

Link to post
Share on other sites

You can check using the image name instead of PID. If it's correct that only a single instance of this process should be running then only one vlc.exe should exist at once. Otherwise you'll need to remake the loop and use different way to determine what the user can see when clicking the tray icon. Maybe by processing the TrayGetMsg() messages.

Edited by Authenticity

Share this post

Link to post
Share on other sites

Thanks for the reply Authenticity. After strategically placing a few traytips to debug the script, I realised that although clicking "VLC" does spawn a new process, they immediately close, so the initial PID remains throughout. I don't know why I didn't notice this behaviour sooner, it makes the whole process much simpler in just keeping track of the initial PID :)

Cheers from me,

Hope this helps out someone in the future...


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