I discovered that many hours of data was never written to the file. So I explored, and researched A_LastError. I found out a system error of 183 indicates the file exists, so AHK fails to create it, and is instead opening it to write it. This is normal behavior for AHK as far as I can tell when using Append mode. I found out the lockout error is Error 32. In the event other errors arose, I opted to check that A_LastError is not 183 instead of just checking for A_LastError=32. (As a bonus, I also wanted to detect when A_LastError is zero, as that meant it was a brand new file I was writing, and that I wanted to put a header to it.) If I get an error besides 183, I am writing to a brand new file.
else If A_LastError != 183
csv:=FileOpen("Backup " A_DD A_Hour A_Min " .csv",2)
Then I would go onto the rest of my code with
Then I end it off with
I think this is a result of my computer managing to fall asleep; I haven't tested if the code can still run when sleeping, but I'm pretty sure on the occasions that I wake my computer up (I've set it to never sleep when plugged in) it starts to create these Backup files.
And it creates a series of them until I restart my computer to clear the lock on the main files.
I have researched using Resource Monitor (windows 8/8.1/10 native app apparently) to identify a locked file. But searching the names of these files gets me no results. Searching for a file opened in Excel, as a test, does find the file as locked by Excel. Which means either Resource Monitor is faulty and can't find AHK processes hiding in memory, or something has happened and when AHK's process terminated it did not release the files.
What can I do to ensure AHK releases the lock on the files?