By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 1,927 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

File Sharing and multiple file handles

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

Tony Liu wrote:
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


Nov 15 '05 #2

P: n/a
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
"Antenna" <q1****************@hotmail.com> wrote in message
news:bv**********@news.worldonline.be...
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.

Tony Liu wrote:
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

Nov 15 '05 #3

P: n/a
And why can't you share a single master file handle?

Tony Liu wrote:
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


Nov 15 '05 #4

P: n/a
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.

"Antenna" <q1****************@hotmail.com> wrote in message
news:c0**********@news.worldonline.be...
And why can't you share a single master file handle?

Tony Liu wrote:
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

Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.