File Read/Write Problem 
Author Message
 File Read/Write Problem

Hi all,

I have to read an integer from a disk file, increment and write it back again in
C#. Some sort of system wide persistent counter.

This can be done with the StreamReader and StreamWriter objects, ok. But i
cannot risk to close the file between the read and write operation as there
might be multiple processes try to increment the value concurrently...

Any ideas?

Thanks a lot,
ulli



Mon, 28 Feb 2005 00:32:43 GMT  
 File Read/Write Problem
Ulli,
All you have to do is set "no sharing" when a process attempts to open the
file.  Detect a sharing violation and retry with a slight delay.  For
example, the code can loop up to X number of times to open the file.  If an
error other than a sharing violation occurs, break from the loop immediately
since it's probably something that isn't going to resolve itself.

Paul


Quote:
> Hi all,
> ...i cannot risk to close the file between the read and write operation as
there
> might be multiple processes try to increment the value concurrently...

> Any ideas?

> Thanks a lot,
> ulli



Mon, 28 Feb 2005 01:36:57 GMT  
 File Read/Write Problem
Hi Ulrich,

If your question relates to how to rewind the stream to rewrite the new
value without closing it, then the following might help...


FileAccess.ReadWrite, FileShare.None);

   BinaryReader reader = new BinaryReader(file);
   BinaryWriter writer = new BinaryWriter(file);

   int i = reader.ReadInt32();

   writer.Seek(0, SeekOrigin.Begin);
   writer.Write(i+1);
   writer.Flush();

   file.Close();

Good luck

Quote:

> Hi all,

> I have to read an integer from a disk file, increment and write it back again in
> C#. Some sort of system wide persistent counter.

> This can be done with the StreamReader and StreamWriter objects, ok. But i
> cannot risk to close the file between the read and write operation as there
> might be multiple processes try to increment the value concurrently...

> Any ideas?

> Thanks a lot,
> ulli



Mon, 28 Feb 2005 03:26:43 GMT  
 File Read/Write Problem
Hi Greg,

Thanks for your answer. But, i'm sorry, I don't believe i have to use a mutex
because of
- Accessing the file is usually done in the foreground process, so there is no
need to synchronize the file access between various program parts,
- Access coordination is required only between processes (.exe's) in my case,
- I'm not sure if i can use the monitor class for inter-process synchronization
- The file itself works as a mutex. If a process should fail opening the file, i
consider a thread.sleep(...) for some milliseconds and a subsequent retry
sufficient in this case.

Thanks a lot,
ulli



Mon, 28 Feb 2005 08:01:59 GMT  
 File Read/Write Problem
Hi Jeremy,

I must have been done wrong something, because this approach I have tried first.
Maybe because of some other reason - your approach works perfectly for my
purpose.

Thanks a lot,
ulli



Quote:
> Hi Ulrich,

> If your question relates to how to rewind the stream to rewrite the new
> value without closing it, then the following might help...


> FileAccess.ReadWrite, FileShare.None);

>    BinaryReader reader = new BinaryReader(file);
>    BinaryWriter writer = new BinaryWriter(file);

>    int i = reader.ReadInt32();

>    writer.Seek(0, SeekOrigin.Begin);
>    writer.Write(i+1);
>    writer.Flush();

>    file.Close();

> Good luck


> > Hi all,

> > I have to read an integer from a disk file, increment and write it back
again in
> > C#. Some sort of system wide persistent counter.

> > This can be done with the StreamReader and StreamWriter objects, ok. But i
> > cannot risk to close the file between the read and write operation as there
> > might be multiple processes try to increment the value concurrently...

> > Any ideas?

> > Thanks a lot,
> > ulli



Mon, 28 Feb 2005 08:05:00 GMT  
 File Read/Write Problem
A mutex can be system wide, not just process specific.  If you give a
mutex a name, that mutex will exist for the entire machine, not just
for the application that created it.  Therefore, you can use a mutex
to synchronize access to a shared resource across multiple
applications.

Jonathan Schafer

On Thu, 12 Sep 2002 02:01:59 +0200, "Ulrich S{*filter*}"

Quote:

>Hi Greg,

>Thanks for your answer. But, i'm sorry, I don't believe i have to use a mutex
>because of
>- Accessing the file is usually done in the foreground process, so there is no
>need to synchronize the file access between various program parts,
>- Access coordination is required only between processes (.exe's) in my case,
>- I'm not sure if i can use the monitor class for inter-process synchronization
>- The file itself works as a mutex. If a process should fail opening the file, i
>consider a thread.sleep(...) for some milliseconds and a subsequent retry
>sufficient in this case.

>Thanks a lot,
>ulli



Mon, 28 Feb 2005 09:42:15 GMT  
 File Read/Write Problem
Hi Ulrich,

No problem - glad to help.

Quote:

> Hi Jeremy,

> I must have been done wrong something, because this approach I have tried first.
> Maybe because of some other reason - your approach works perfectly for my
> purpose.

> Thanks a lot,
> ulli



>>Hi Ulrich,

>>If your question relates to how to rewind the stream to rewrite the new
>>value without closing it, then the following might help...


>>FileAccess.ReadWrite, FileShare.None);

>>   BinaryReader reader = new BinaryReader(file);
>>   BinaryWriter writer = new BinaryWriter(file);

>>   int i = reader.ReadInt32();

>>   writer.Seek(0, SeekOrigin.Begin);
>>   writer.Write(i+1);
>>   writer.Flush();

>>   file.Close();

>>Good luck


>>>Hi all,

>>>I have to read an integer from a disk file, increment and write it back

> again in

>>>C#. Some sort of system wide persistent counter.

>>>This can be done with the StreamReader and StreamWriter objects, ok. But i
>>>cannot risk to close the file between the read and write operation as there
>>>might be multiple processes try to increment the value concurrently...

>>>Any ideas?

>>>Thanks a lot,
>>>ulli



Mon, 28 Feb 2005 15:34:35 GMT  
 File Read/Write Problem
Ulrich, Jonathan addressed the technical side of your questions.

In general your file will work as a mutex as you say.  You will find though
that using a Mutex will make for better code.  When I say better I mean
using thread synchronization is considered a best practice when trying to
synchronize thread's access to a resource.  Synchronization classes like
Mutex are built and designed for just this purpose.

--
Greg
http://www.claritycon.com/


Quote:
> Hi Greg,

> Thanks for your answer. But, i'm sorry, I don't believe i have to use a
mutex
> because of
> - Accessing the file is usually done in the foreground process, so there
is no
> need to synchronize the file access between various program parts,
> - Access coordination is required only between processes (.exe's) in my
case,
> - I'm not sure if i can use the monitor class for inter-process
synchronization
> - The file itself works as a mutex. If a process should fail opening the
file, i
> consider a thread.sleep(...) for some milliseconds and a subsequent retry
> sufficient in this case.

> Thanks a lot,
> ulli



Mon, 28 Feb 2005 21:45:29 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. File reading/writing problem

2. File read/write problems

3. Problem opening a file for read while it is open for write

4. PROBLEM READING/WRITING FILE __SIMULTANEOUSLY__!

5. file reading/writing speed problem

6. problem reading from and writing to file - please help

7. problem reading/writing text file from disk

8. Reading a file / Write what you read

9. Reading a pattern of string from text file and writing it to a new file

10. reading a string pattern from a file and writing it to another file

11. Outlook *.msg file format and read / write *.pst file

12. File reading problem - reading a text colormap.

 

 
Powered by phpBB® Forum Software