Replace "Render button/max quick render" sequence with custom function?

I've almost completed a scripted plugin, but Max's default render does not update the scene between frames as I need it to. I've been able to solve this by writing my own function to render animations, and would like to be able to drop it in place of the standard sequence.

Looking back at I discovered it is possible to halt and cancel Max's render sequence. However, when I try to run the following, for example:

dialogMonitorOps.unregisterNotification id:#test
dialogMonitorOps.enabled = false
fn customRender = (
	local hwnd = dialogMonitorOps.getWindowHandle()
	local hwndText = uiAccessor.getWindowText hwnd
	if classOf hwndText == string do (
		local substr = substring hwndText 1 9
		if substr == "Rendering" do (
			-- Cancel standard render
			uiAccessor.pressButtonByName hwnd "Cancel" 
			-- Substitute scripted render
			render() -- PLACEHOLDER
dialogMonitorOps.enabled = true
dialogMonitorOps.interactive = false
dialogMonitorOps.registerNotification customRender id:#test

I get the following:

-- Error occurred in customRender(); filename: C:\Users\Kevin\Documents\3dsMax\scripts\customRend; position: 462; line: 16
-- Frame:
-- hwndText: "Rendering"
-- substr: "Rendering"
-- hwnd: 5444878P

To save the trouble of counting, that's the line which in this case simply says render()
Obviously that works when run outside the function, and Max doesn't seem to want to tell me what the error actually IS. Any tips on getting this working?