Can't close a secret database

In a file called PortalFile, I have this code:

let SelectedInvoice = InvoiceNumber
opensecret "Invoices"
    setactivedatabase "Invoices"
    select InvoiceNumber = SelectedInvoice
    InvoiceDataArray = arrayselectedbuild(cr(), "Invoices", 
        {InvoiceNumber +","+ InvoiceDate +","+ CustomerID +","+ CustomerName +","+ Salesperson +","+ Quantity +","+ ProductCode +","+ TotalPrice +","+ InvoiceTotal}
    closedatabase "Invoices"
    message info("files") 

This is the message I get:

Screen Shot 2021-11-20 at 4.11.25 pm

The correct data has been placed in the previously defined global variable, InvoiceDataArray,

I’ve solved the problem by inserting a save statement in front of one of the close statements and deleting the other one. But that doesn’t answer the question of why the file remains active.

I notice both databases are listed in your message alert. Did the window, or windows for the Portal file close, or did they remain open? When I tried opening a file of my own in secret, then closing it, and then the active database, only the active database was listed in the message. The secret database was not. The windows for the active database had closed however, and when I checked Memory Usage, neither database was listed.

I think, in my case at least, both databases closed, but there was a lag in updating the list of open files.

The PortalFile window remained open. I assume that neither of the close statements did anything and the secret file remained the active one (and all subsequent statements failed because they were intended for the PortalFile but were being applied to the secret file). The save idea came from the documentation. In fact, I have a vague recollection of having written that snippet in the long ago, following a forum exchange, but I can’t be certain.

Presumably, either the select statement or the arrayselectedbuild( function triggered the problem. I’ll make a note to test those when I have more time.

And you’re up late Dave - been out on the town?

That makes sense. The closedatabase and closefile statements call an Apple API to close the document. Though Panorama’s code does not use multiple threads, Apple’s document code does. So the process of closing the database is not guaranteed to be finished when the next statement runs, in fact it probably won’t be finished until sometime later.