Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
setfile errors
#1
i have a function that is monitoring file access in a directory. when a file is accessed it copies the name and time over to a text file. it works fine unless there are a lot of access events (like a massive copy process). when this happens i get this error.

"The process cannot access the file because it is being used by another process."

Is there a way to reliably to do this even for massive file triggers?
An old blog on QM coding and automation.

The Macro Hook
#2
Probably two threads call setfile simultaneously. Tomorrow I'll try to find the best solution.
#3
Ahhh.. I tried this a while back too. I was trying make a cheap and easy way to keep a hard drive mirrored. It worked fine until I started a massive number of file moves or copies. It'd be great to have a solution but there's more tuned software for this.

Matt B
Matt B
#4
i've kind of found a work around till we can get a "wait if file is locked" function.

Code:
Copy      Help
err
,double z=Uniform(1 1000)
,wait (z/1000)
An old blog on QM coding and automation.

The Macro Hook
#5
err is OK. Another simple solution - use mutex.

This example reproduces the error.

Macro
Code:
Copy      Help
mac "setfile_error"
mac "setfile_error"

Function setfile_error
Code:
Copy      Help
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.

Code:
Copy      Help
;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)

Code:
Copy      Help
;execute this when finished, eg before unloading QML file
CloseHandle g_mutex1; g_mutex1=0

Function setfile_error
Code:
Copy      Help
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.
#6
QM 2.2.0.6

Just place lock before, and lock- after. Code inbetween will not be executed by two threads simultaneously.
#7
this seems to work great!
thanks.
An old blog on QM coding and automation.

The Macro Hook


Forum Jump:


Users browsing this thread: 1 Guest(s)