Jump to content

Trayitemsetonevent With Dynamic Function / Argument ?


Recommended Posts

Hi again.

Thinking ahead to include a possible "modular" structure / "plugin" system to a script, i have a section that reads information from an INI file, and creates Tray items based on this information. Of course, just to make life difficult, the INI was to specify a scriptname to run also... and here is the problem.

[MODULE]

Name=First One

File=1.au3

 

; there is a loop reading INI files until completed, incrementing $i

; $Tray[] is used to be able to define a tray event for each tray menu item

$sTrayText = IniRead("filepath etc etc","MODULE","Name","")

$fName = IniRead("filepath etc etc","MODULE","File","")

$Tray[$i] = TrayCreateItem($sTrayText)
TrayItemSetOnEvent(-1,"RunModule(" & fName & ")");can't parse this. bad syntax or not possible ?

Func RunModule($filename)

;do something with the file

EndFunc
The process fails on setting the TrayItemSetOnEvent, as the function is undefined, because the script is looking for a function called 'RunModule(" & fName & ")")' instead of 'RunModule()'

Any takers ? Cheers :think:

Link to comment
Share on other sites

maybe

Dim $fName = IniRead("filepath etc etc","MODULE","File","")

$Tray[$i] = TrayCreateItem($sTrayText)
TrayItemSetOnEvent(-1,"RunModule");can't parse this. bad syntax or not possible ?

Func RunModule()
    Run($fName)

;do something with the file

EndFunc

8)

NEWHeader1.png

Link to comment
Share on other sites

I usualy use somthing like this:

$sTrayText = IniRead("filepath etc etc","MODULE","Name","")
$fName = IniRead("filepath etc etc","MODULE","File","")
$Tray[$i] = TrayCreateItem($sTrayText)
;TrayItemSetOnEvent(-1,"RunModule(" & fName & ")");can't parse this. bad syntax or not possible ?
while GuiGetMsg() <> -3
    TaryMsgCracker(TrayGetMsg())
    Sleep(100)
WEnd
Exit
;;
Func TrayMsgCracker($trayMsg)
    if $trayMsg <> 0 
        switch $trayMsg
            case $Tray[$i]
                RunModule($fName)
            case Else
            ;;
        EndSwitch
    EndIf
EndFunc

Func RunModule($filename)
;do something with the file
EndFunc
Link to comment
Share on other sites

Thanks Valuater. The problem is (which i appreciate isn't illustrated well in the example code) is that the tray item setup occurs in a loop - the code is run a variable number of times... thus, each round of the loop needs to create its own onevent handler... and the handler needs to call a function passing the filename argument to it... Thus, even if i create a Global array that is populated with the filepath to the various script modules, the execution function (ie 'RunModule' still needs to have the index passed to it when the tray menu item is created... and that is the problem.

I have a suspicion that i am going to need to come up with an alternative way round this... but i will try to create the string using CHR() etc to see if it parses OK then...

@Uten - thanks for your example. An additional "issue" is that the tray system doesn't run in a loop - there is another loop elsewhere - all events on the tray icon / menu are handled with onevent - setting up a loop to monitor the tray menu items won't allow the main loop to start (i know, i know - what a pain in the a@# ! With the main loop (loop in a loop in a loop) integrating "listening" to tray GUI events would be a real pain...)

Edited by plastix
Link to comment
Share on other sites

I like this one

TaryMsgCracker(TrayGetMsg())

8)

Ouch, :"> Ok, did not take the time to run this sample, but I use this technique to get what TP wants.

If @plastix wants a connection between the stuff read in the ini and the menu event handler he could use a array to save the file names in.

Func TrayMsgCracker($trayMsg)
    if $trayMsg <> 0
        switch $trayMsg
            case $trayItem1
                Handel1()
            case Else
                $i = 0 to UBound($Tray) -1
                if $trayMsg = $Tray[$i] Then 
                   RunModule($fName[$i])
                endif
        EndSwitch
    EndIf
EndFunc
Link to comment
Share on other sites

Many thanks to all. Uten's solution requires a GUIGetMsg / TrayGetMsg loop - which i can't have. Infact, my script loop runs a completely different system - the TRAY ICON / MENU is a seperate entity with some infoboxes etc - all run non-modal and do not pause the main loop running. To integrate checking the Tray Items for events into the main loop would require many repeated code snippets etc etc...

The solution to a pseudo-multi threaded system uses the following:

$i = 0
$FILEHANDLE = FileFindFirstFile(@ScriptDir & "\Modules\*.ini")
If $FILEHANDLE = 1 Then
While 1
$FILENAME = FileFindNextFile($FILEHANDLE)
If @error Then ExitLoop
$i = $i + 1
$TrayItem = IniRead(@ScriptDir & "\Modules\" & $FILENAME,"MODULE","Name","")
$Temp[$i] = IniRead(@ScriptDir & "\Modules\" & $FILENAME,"MODULE","File","")
$Tray[$i] = TrayCreateItem($TrayItem)
TrayItemSetOnEvent(-1,"RunModule")
Wend
$Tray[0] = $i
EndIf
TrayCreateItem("")

 

Func RunModule()
$event = @TRAY_ID ;TrayGetMsg will be 0 as have to use TrayItemSetOnEvent()
If $event <> 0 Then
 Switch $event;actually, could set a loop for $Tray[loop] = $event then Run $Temp[loop] etc...
 Case $Tray[1]
  MsgBox(0,"Tray Item 1",":)")
 Case Else
 EndSwitch
 EndIf
EndFunc

Thanks to all for the pointers. This allows for independant tray events from main loop :think:

Link to comment
Share on other sites

Wish you the best of luck :(

What I raed out of the things you try to explain, what I think you want, and the way you want to achive it sortof guves me a hunch that your on realy, realy deep water :think: Realy no offence, you can learn a lot on deep waters :)

Any how, you know about xml-rpc and soap?

Link to comment
Share on other sites

Hi Uten

I know what you mean. Diving in at the deep end is the best way to learn :( I see how most things are set up - but am trying to emulate the non-modal environment i have used with VBA etc...and the above tecnique does that. it lets me seperate a modular menu system from the main script loop.

Everybody's help is invaluable to learning. Thanks all again.

PS - i'll worry about SOAP etc another time :think:

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