File Sharing and multiple file handles

  • Thread starter Thread starter Tony Liu
  • Start date Start date
T

Tony Liu

Hi, how can I create multiple new file handles of a file without having to
share to file to the other processes?
I have a file that will be accessed by multiple threads in my application,
each time a thread try to do something with the file, the thread will create
a new file handle. However, if I specify FileShare.ReadWrite, other process
can also open that file. I tried FileShare.Inheritable but it doesn't work.

The reason I needs to create a new file handle each time is because those
different threads will be working on a different area of the file and I just
can't share a single master file handle.

Thanks in advance
Tony
 
Amazing what sort of stunts programmers always want to do :)

As I understand: you app has different threads. Each thread can modify
The One and Only file. But if one thread is busy with that file, all
other threads should have R access to that file. So, there's always only
one thread with RW access - or none. Is that correct? If so, I would
think you have some sort of mechanism to synchronise the access to that
file. And if you have such a mechanism, only one thread will be able to
write to that file. So, what is the problem then? Try describing your
problem in more detail.
 
Actually, all threads have RW access to the file. The file contains paged
data, therefore, one thread could be reading from page 1, while another
thread is writing to page 2, and also possibly another thread is writing to
page 3, etc.

My problem is that, since I must create a new file handle so that all thread
can have their own file pointer(they are working in different blocks of data
in the file), I must set the FileShare to FileShare.ReadWrite. Now this
actually works, but what I really want is to only allow RW sharing within my
process, and have the file locked from other process, so that other process
cannot open it while my process is working with the file.

Thanks
Tony Liu
 
Because more than one thread could access the file at the same time, if
thread A needs to read from the beginning of the file and thread B needs to
write to the end of the file at the same time, they need to have their own
handle instead of sharing the master file handle.
 
Back
Top