File selection pop-up compatibility with system.util.invokeAsynchronous()

Hi,

I’m using the system.dataset.exportExcel() function to export a large dataset to an Excel file.
Since it takes a while to save the file, I’m calling it in a function using system.util.invokeAsynchronous().
It appears to work fine, but I wanted to make sure that it is thread safe.
My concern is that the exportExcel() function pops-up the file name selection pop-up in the GUI for the user to enter a file name.
Does the violate the warning in the invokeAsynchronous() documentation of ‘Under no circumstances should you ever do anything in the function that is invoked asynchronously that interacts with the GUI.’ ?

Thank you for your help,

John

The “safe” thing to do is to use system.util.invokeLater (even from an async thread) to push your new event to the back of the event queue. You would probably get away with this regardless, because file choosers in Swing are modal (meaning everything else on the EDT is halted while you’re saving a file) but invokeLater is the right way to do this.

Thank you for the quick response!
I was afraid that it wasn’t the best way to do it.
I’m in a catch-22 because the file save is what I would like to do in the background because it is taking a while to save to the user’s computer in a Citrix environment. Oh well if the user wants to save the file they can wait for it.

Ah - sorry, think my statement was a bit confusing. You’re totally fine to process on a background thread - the important thing is that before calling savefile, you use invokeLater (from your background thread) to get back onto the event dispatch thread. Something like:

def slowFunction():
    # do something slow
    results = something
    def saveFile(results=results):
        system.dataset.exportExcel(results)
    system.util.invokeLater(saveFile)

system.util.invokeAsync(slowFunction)

Thank you for the clearification.
I think my situation is a bit unusual in that both processing the data to create the data set and saving the file both take several seconds.
When I follow your suggestion, the data processing is done in the background but writing the file to disk still locks up the GUI for the time required to write the file from the Citrix environment to the users hard drive.
Ideally, I would ask the user for the desired file name in the GUI thread when he presses the export button and then process the data and write the file to disk in the background.

I mean, you certainly could do that - use system.file.saveFile to get the filename to save (before starting the async operation) and then system.dataset.datasetToExcel and system.file.writeFile to actually save it to disk.

I’ve always been using the system.dataset.exportExcel. I didn’t realize that there was another option. That will work perfect. It will be in the background and safe. Thank you!!

1 Like