Howto lock a directory

  • Thread starter Thread starter Frederik Jensen
  • Start date Start date
F

Frederik Jensen

Hi,

How do I lock a directory for access in a multithreaded application?
I have Thread1 and Thread2. Thread1 wants to delete a directory. The thread
checks to see whether or not the directory is empty. If it is empty it
deletes the directory. However, Thread2 wants to put files in the directory.
The problem is when this occurres:

1) Thread1 finds that the directory is empty
2) Thread2 puts a file in the directory
3) Thread1 tries to delete the directory, but an exception is thrown as the
directory is not empty.

I have looked at the System.Threading.Monitor class but cannot figure out
how to lock the directory as it cannot be passe into the Enter method.

Any ideas of how to accomplish this otherwise simple matter.

In advance, thx.
 
Sure, forget about trying to synchronize on the directory and synchronize on
a mutex, instead. Each thread can only do its operation when it owns the
mutex.

Paul T.
 
Paul G. Tobey said:
Sure, forget about trying to synchronize on the directory and synchronize
on a mutex, instead. Each thread can only do its operation when it owns
the mutex.

Thanks for the tip. It works great.
 
Back
Top