[Closed] dotNetObject and event handler inside ca
I’ve run into this weird issue with a script I’m working on where using dotNet.addEventHandler doesn’t do anything when invoked from inside a ca’s event.
Below is a simplified script to illustrate what I’m trying to accomplish. In the original script the on TestParam set val actually calls a function that creates a dotNetObject (unless it already exists) and attaches several event handlers. However, to find out what was going wrong I wrote this simple test script:
fn Tmr_Tick =
(
print "Tick!"
Tmr.Stop()
)
TestCa = attributes "TestCa"
(
parameters params
(
TestParam type:#integer
on TestParam set val do
(
Tmr = dotNetObject "System.Windows.Forms.Timer"
dotNet.addEventHandler Tmr "Tick" Tmr_Tick
Tmr.Interval = 100
Tmr.Start()
)
)
)
TestObj = box()
custAttributes.add TestObj TestCa
After running the above script, enter line by line in the listener:
Tmr
undefined
TestObj.TestParam = 1
1
[i]no "Tick" ?[/i]
Tmr
dotNetObject:System.Windows.Forms.Timer
Tmr.Enabled
true
So at this point the timer is created and is running, but the Tmr_Tick function is never called.
Adding the event manually in the listener after this:
dotNet.addEventHandler Tmr "Tick" Tmr_Tick
OK
"Tick!"
Any ideas would be very much appreciated!
Thanks,
Martijn
no idea – seems like a bug ( your post pretty much has the exact findings in this earlier thread: http://forums.cgsociety.org/showthread.php?t=728464 )
hi Martijn,
Looks like the same issue as me – i think it has something to do with the eventhandler being added in global scope, otherwise it doesnt register.
In my case i was using the flowlayoutpanel – when dynamically adding controls, they wouldnt register. I got around this by building a custom control that registered these within the class library. This way, it worked within the Ca def in the command panel, but had the downside of needing a loaded assembly.
I’ve logged this with autodesk.
Thanks for the replies guys Hadn’t noticed that thread ( search button? where? )
Cheers,
Martijn
I had the same problem once when adding event handlers to picture boxes I solved it by having a global array with all the picboxes and then adding the eventhandlers in a global scope also… In your case it should be something like this:
mTimer=#(dotNetObject "System.Windows.Forms.Timer")
fn Tmr_Tick sender args =
(
print "Tick!"
sender.Stop()
)
TestCa = attributes "TestCa"
(
parameters params
(
TestParam type:#integer
on TestParam set val do
(
mTimer[1].Interval = 100
mTimer[1].Start()
)
)
)
dotNet.addEventHandler mTimer[1] "Tick" Tmr_Tick
TestObj = box()
custAttributes.add TestObj TestCa
Hope it helps, cheers!