[Closed] Protecting dotNet component events from GC in max 2009
I know of the dotNet.setLifetimeControl option in max 2010 and above. Is there any alternative for max 2009?
The component in question is not a dotnetcontrol, it’s a webclient.
I would be interested in this as well. So far I had to change my backwards compatibility from 2008 to 2010 because of this
How and why do you want to use dotnet.setLifetimeControl? What the actual problem is? Does the Webclient lose event or dispose? What does cause the problem (gc, destroying the main window, etc.)?
There are several tricks how to protect dotnetobject in MAX without using dotnet.setLifetimeControl.
He’s probably having the same issues I was.
For me, the buttons tooltips would still work, but after I ran a function from a dotnet button, the rightclick/left click/on enter/leave handlers would be gone.
My issue is when transferring large files to ftp asynchronously via webclient (or any other solution for that matter, I’ve also tried with 3rd party FTP class libraries) the FTP closes the connection midway for some reason. Smaller files pass through just fine.
I was not 100% sure my issue is GC-related. I was trying to rule that out, though at this point I am more convinced the issue lies in network connectivity issues or local machine settings, as it doesn’t seem to affect all machines.
But in general, would be nice to know how to protect dotnet event handlers if you have any tips as I’ve suffered from those issues as well in the past.
i’m pretty far from these things right now and don’t really have a time to dig into it. as you find a solution please share it with us
Here’s a test. If you click on the button, it creates a box in the viewport. After a few clicks, the Mouse Down/Enter/Leave events are gone.
fn testFunction =
(
box()
)
fn onMouseEnter ctrl evnt =
(
setfocus dotnetEventRoll.flp
Print "Mouse Enter"
local Info = ctrl.name
local ToolTipObj = dotnetobject "System.Windows.Forms.ToolTip"
(ToolTipObj.SetToolTip ctrl Info)
)
fn onMouseLeave ctrl evnt =
(
print "Mouse Leave"
)
fn onMouseDown ctrl evnt =
(
print "Mouse Down"
testFunction()
redrawViews()
)
rollout dotnetEventRoll "Dotnet Event Test"
(
dotNetControl flp "flowlayoutpanel" pos:[0,0] width:90 height:200
on dotnetEventRoll open do
(
local thumbArray = #()
local btndragdrop = dotnetobject "button"
btndragdrop.font = dotNetObject "System.Drawing.Font" "Arial" 200 --myFontStyle
btndragdrop.size = dotnetobject "system.drawing.size" 33 33 --bitInfo[3] bitInfo[4]
btndragdrop.margin = dotnetobject "system.windows.forms.padding" 1 -- thumbs distance
btndragdrop.name = "Tooltip Test"
btndragdrop.text = "Test"
dotnet.addEventHandler btndragdrop "MouseEnter" onMouseEnter
dotnet.addEventHandler btndragdrop "MouseLeave" onMouseLeave
dotnet.addEventHandler btndragdrop "MouseDown" onMouseDown
btndragdrop.AllowDrop = true
append thumbArray btndragdrop
btndragdrop = nothing
flp.controls.addrange thumbArray
flp.AutoSize = true
flp.autoscroll = true
flp.padding = dotnetobject "system.windows.forms.padding" 0
flp.WrapContents = true
)
)
createdialog dotnetEventRoll 90 200
that’s an exact sample how people hope for the magic of setLifetimeControl instead of doing things just right. In your sample you create a button inside rollout’s open event. So this button doesn’t really keep reference to the rollout. After GC the button has to lose events. Only thing that I did was the defining of button variable in local scope of the rollout. It makes the button as a part of LIVE control (rollout). In this case the button can’t be disposed by GC.
try(destroydialog dotnetEventRoll) catch()
fn testFunction = (box())
fn onMouseEnter ctrl evnt = (format "mouse enter: %
" ctrl.text)
fn onMouseLeave ctrl evnt = (format "mouse leave: %
" ctrl.text)
fn onMouseDown ctrl evnt = (testFunction(); format "mouse down: %
" ctrl.text)
rollout dotnetEventRoll "Dotnet Event Test"
(
dotNetControl flp "flowlayoutpanel" pos:[0,0] width:100 height:30
local btndragdrop
on dotnetEventRoll open do
(
btndragdrop = dotnetobject "button"
btndragdrop.text = "Test"
dotnet.addEventHandler btndragdrop "MouseEnter" onMouseEnter
dotnet.addEventHandler btndragdrop "MouseLeave" onMouseLeave
dotnet.addEventHandler btndragdrop "MouseDown" onMouseDown
flp.controls.addrange #(btndragdrop)
)
)
createdialog dotnetEventRoll 100 30