Notifications
Clear all

[Closed] Using dotnet timer to destory dialog

I’m trying ot use a dotnet timer to close a dialog after x amount of time…it’s to restrict a piece of trial software to short sessions. I was previously using a max timer, but that’s too easy to circumvent, I assumed a dotnet one would be more ‘secure’. However, I can’t seem to get it to work… I get MAXscript Rollout Handlers Exeptions – unknown system exception.

Here’s the test code, which should shut after 5 seconds.

global test
 rollout test "Testing"
 (
 	   local myTimer = dotnetobject "System.Timers.Timer" 5000 autoreset:true
 	 
 	fn onElapsed sender args =
 	(
 		myTimer.Stop()
 		dotnet.removeAllEventHandlers myTimer
 		myTimer.Dispose()
 		destroydialog test
 	)
 
 	on test open do
 	(
 		dotnet.AddEventHandler myTimer #elapsed onElapsed
 		dotNet.setLifetimeControl myTimer #dotnet
 		myTimer.Start()
 	)
 
 )
 try (destroyDialog test) catch()
 createDialog test

I’m not really sure about dotnet stuff in general, so I could be approaching this wrong.
Beware… this code seems to my max unstable.

Any help apprciated…

4 Replies
 lo1

try this as a workaround:

global test
 rollout test "Testing"
 (
	timer maxTimer interval:1 active:off	 
	on maxTimer tick do destroyDialog test
	 
 	   local myTimer = dotnetobject "System.Timers.Timer" 5000 autoreset:true
 	 
 	fn onElapsed sender args =
 	(
 		myTimer.Stop()
 		dotnet.removeAllEventHandlers myTimer
 		myTimer.Dispose()
                maxTimer.interval = 1 --in case it was tampered with
 		maxTimer.active = on
 	)
 
 	on test open do
 	(
 		dotnet.AddEventHandler myTimer #elapsed onElapsed
 		dotNet.setLifetimeControl myTimer #dotnet
 		myTimer.Start()
 	)
 
 )
 try (destroyDialog test) catch()
 createDialog test
1 Reply
(@rorschach)
Joined: 11 months ago

Posts: 0

Nice solution, I love that you use the max timer, the one thing I was avoiding to make it work. I wonder if there’s a cleaner method? Why would it be that you can’t close the dialog with the dotnet function? Anyone?

maybe moving the creation of dotnet timer inside the on-open handle w’d make it more sequre

 lo1

There a lot of functions in max that need to be called directly from the max UI thread. The dotnet timers (system.timers.timer and system.threading.timers, I think windows.forms.timer is ok) are executed on a different thread.