Screenshot Using Robot().CreateScreenCapture

Using ignition 8.1.27 vision

I have a button that creates a screenshot of the open program. I recently changed the entire script from a project script that created a PDF by sending the image through SQL to a button script that makes a JPEG instead using the following code:

The old way of sending the image through SQL was causing temporary communication losses with the client/gateway. I thought the changes I made would stop this but it seems I still have comm losses. When I tried to change the button script to a gateway project script it works the first time the button is pressed, then it stops working until the program is closed/ reopened. It seems to work back to back as a button script - but will eventually cause comm loss. Am I missing something to close out the image once created? I know that in both scenarios (project script or button script) the code is reaching the end because the last line creates an event log that I can see, but with the project script it won't execute after the first time

It looks like you have cut off part of your code at the bottom, post code, not pictures of code. Please see Wiki - how to post code on this forum.

Other points of note:
-This doesn't appear to be a project library script, as I'm not seeing any function def anywhere.
-Don't use exit(), full stop. If you need to exit your logic early, put your logic in a function in the project library and use return. Call said function from your button.
-Ignition provides built in methods for both reading and writing to files. Use system.file.writeFile instead of ImageIO.write.
-In general, use built in Ignition methods before deferring to java or python packages (especially with times/dates).

Project scripts only ever run once (per project save/update). You should be defining constants in the project library plus functions that do the actual work. Your event should call a function in the library script.