Refactored the PLC demo window closing logic. This now avoids the possible infinite loop from the previous version if the window never appears.