Designer hanging with script, but client runs fine

The following (section) of a script i’m working on will run fine (doesn’t hang) running as a client, but when running it in the designer, designer hangs indefinitely if the print statement commented bellow is not here. I’ve tried replacing it with sleep() and it still hangs. I’m not sure why it’s working with the print statement there but not when it is gone.

What i am doing is writing the time to a text box that is inside a template instance. If i write to the text property of a text box which is not inside a template, it does not hang. This was working fine at one point but i made some changes to the template (which i cannot seem to figure out what exactly did this) now it hangs.

				for component in container.components:
					componentName = tagName + '_' + alarmName
					if(component.name == componentName):
						for component in component.components:
							if(component.name == 'SnoozeControls'):
								for component in component.components:
									if(component.name == 'SnoozeStatus'):
										print('*') # If this print statement is not here, designer hangs
										component.text = str(hrTime)

You are using component as your loop variable in all of the nested loop. I would not expect it to ever work correctly.

I was being a bit lazy there, i have made them unique, still having the same issue. I did just notice that it the script doesn’t hang if i stay in the script editor, as soon as i switch over to the vision window that is when it hangs.

				for component in container.components:
					componentName = tagName + '_' + alarmName
					if(component.name == componentName):
						for component1 in component.components:
							if(component1.name == 'SnoozeControls'):
								for component2 in component1.components:
									if(component2.name == 'SnoozeStatus'):
										print('*')
										component2.text = str(hrTime)

Here is the entire function, which runs every 1 second.

def handleAlarms(shutdown=0):
	from datetime import timedelta
	# This function should be ran periodically from client to handle our snoozed alarms
	alarmBasePath = '[default]Powerhouse_PLC/Alarms/'
	path = '[default]Powerhouse_PLC/Alarms/ProjectMemoryTags/SnoozeTime/'
	results = system.tag.browse(path = '[default]Powerhouse_PLC/Alarms/ProjectMemoryTags/SnoozeTime/', filter = {})
	container = system.gui.getWindow('main').rootContainer
	
	# itterate tags listed in path, checking each ones expire uptime, and then un-snoozing if expireTime < system uptime
	for result in results.getResults():
		path = str(result['fullPath'])
		snoozeTagName = str(result['name'])
		expireUptime = system.tag.read(path).value
		currentUptime = system.tag.read("[System]Gateway/UptimeSeconds").value

		if(expireUptime != 0):

			alarm = snoozeTagName.split("_")
			tagName = alarm[0]
			alarmName = alarm[1]
			
			if(expireUptime <= currentUptime or shutdown == 1):

				print(snoozeTagName + " Alarm Snooze Expired, un-snoozing now")
				setAlarmSnooze(tagName, alarmName, 0)
			else:
				snoozeSecondsRemaining = expireUptime - currentUptime
				hrTime = timedelta(seconds=snoozeSecondsRemaining)	

				for component in container.components:
					componentName = tagName + '_' + alarmName
					if(component.name == componentName):
						for component1 in component.components:
							if(component1.name == 'SnoozeControls'):
								for component2 in component1.components:
									if(component2.name == 'SnoozeStatus'):
										print('*')
										component2.text = str(hrTime)
										
		if(expireUptime == 0):
			alarm = snoozeTagName.split("_")
			tagName = alarm[0]
			alarmName = alarm[1]
			container = system.gui.getWindow('main').rootContainer
						
			for component in container.components:
				if('template' in str(type(component))):
					componentName = tagName + '_' + alarmName
					if(component.name == componentName):
						for component1 in component.components:
							for component in component1.components:
								if(component1.name == 'SnoozeStatus'):
									if(component1.text != 'Not Snoozed'):
										component1.text = 'Not Snoozed'
										

Same issue, you are using component multiple times in nested loops at the bottom of your script.

I will fix that, but at this time that part of the script is not running. I just found that i run the script via a button it does not hang, only when run via client timer script.

You are also using system.tag.browse() in the foreground. Like any other script function that calls out to the gateway, it should be called in a background script, and UI interaction handled with .invokeLater(). See this topic:

I make an exception for button actionPerformed scripts (short ones), as an operator can expect some processing delay upon their command.