Unregistering a viewscallback

Hello,

I’m trying some stuff out to get an other script of me better. But I still can"t solve the unregisterproblem. I can register the viewscallback, but it never gets unregistered even the code is called. Someone has an idea why this is?



macroscript testTimer category:"UIMod"
(
	local theTimer = dotNetObject "System.Windows.Forms.Timer" --create a Timer
	local speedTimer = 1000
	
	fn Tick = 
	(
		print localTime
		gw.text [100,100,1] "This is a test" color:white
		gw.enlargeUpdateRect #whole
		gw.UpdateScreen()
		completeRedraw()
	) --define a MAXScript function to be called by the Timer.	
	
	rollout unnamedRollout "Untitled" width:279 height:203
	(
		button btn_start "Start" pos:[42,24] width:197 height:54
		button btn_stop "Stop" pos:[45,101] width:197 height:54
		
		on btn_start pressed  do
		(
			print "Start"
			theTimer.start()
			
			unregisterRedrawViewsCallBack fn_ModUICallBack
				function fn_ModUICallBack =
				(					
					Tick()
				)
			registerRedrawViewsCallBack fn_ModUICallBack
		)
		on btn_stop pressed  do
		(
			print "Stop"
			theTimer.stop()
-- 			DestroyDialog unnamedRollout
			
		)
		
		on unnamedRollout close do
		(
			print "Close event"
			dotnet.removeEventHandler theTimer "tick" Tick
			
			unregisterRedrawViewsCallBack fn_ModUICallBack
			
			completeRedraw()
		)
	)
	
	on execute do
	(
		print "Execute event"
		dotnet.addEventHandler theTimer "tick" Tick --add ON TICK event hander to call the function
		theTimer.interval = speedTimer --set the tick interval to 1 second (1000 milliseconds)
		
		createdialog unnamedRollout
	)
	
-- 	on close do
-- 	(
-- 		print "Close event"
-- 		dotnet.removeEventHandler theTimer "tick" Tick
-- 	)
)


Hey there 2 things,

Make sure to add an unregister command to the STOP pressed event:

on btn_stop pressed  do
(
	print "Stop"
	theTimer.stop()
	unregisterRedrawViewsCallBack fn_ModUICallBack		
)

Then most important you’ll need to locally declare fn_ModUICallBack at the top with your other locals. That way unregisterRedrawViewsCallBack knows what “fn_ModUICallBack” is.

local fn_ModUICallBack

It seems to be working with those changes :):

This is great. Thank you :slight_smile: