err is OK. Another simple solution - use mutex.
This example reproduces the error.
Macro
mac "setfile_error"
mac "setfile_error"
Function setfile_error
AddTrayIcon
str s="text"
rep 1000 ;;about 10 seconds
,s.setfile("$desktop$\test.txt")
,0.01
Two setfile_error threads run simultaneously. At some moment, thread 2 tries to open the file for writing, but at that time the file is opened by thread 1. Then thread 2 fails.
Now try to use mutex to not allow two threads access to the same code simultaneously.
;execute this before starting all, eg when QML file loaded
dll kernel32 #WaitForSingleObject hHandle dwMilliseconds
dll kernel32 #ReleaseMutex hMutex
int+ g_mutex1
if(!g_mutex1) g_mutex1=CreateMutex(0 0 0)
;execute this when finished, eg before unloading QML file
CloseHandle g_mutex1; g_mutex1=0
Function setfile_error
AddTrayIcon
str s="text"
rep 1000 ;;about 10 seconds
,WaitForSingleObject(g_mutex1 -1)
,s.setfile("$desktop$\test.txt")
,ReleaseMutex(g_mutex1)
,0.01
A mutex is an object that can be owned only by a single thread at a time. WaitForSingleObject checks whether the mutex is currently owned by another thread. If yes, it waits until another thread calls ReleaseMutex or ends. Then it gives the mutex to this thread. That is, code between WaitForSingleObject and ReleaseMutex will never be executed by two or more threads simultaneously.