0

I've a requirement like, there a file with name Log.txt and is a shared file using by two different libraries parent and child in parallel. In parent library we are adding some lines of data after that in child library deleting the same data in same file. But, we cannot close the FileStream in parent because it is keep on adding the data.

Parent code goes like below (Writing the data) :

using (FileStream fileStream = new FileStream(logFilePath, FileMode.Append))
{
    using (StreamWriter log = new StreamWriter(fileStream))
    {
        log.WriteLine(strLog);
    }
}

Child code goes like below (Clearing the data):

using (FileStream fileStream = new FileStream(logFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
    //fileStream.SetLength(0);
    using (StreamWriter log = new StreamWriter(fileStream))
    {
        File.WriteAllText(logFilePath, string.Empty);
    }
}

Here I'm getting error This file is using by some other process. Yes, I know this error will come for which reason. My question is is there any other way to achieve this flow?

Thanks in advance

Rand Random
  • 7,300
  • 10
  • 40
  • 88
Chanikya
  • 476
  • 1
  • 8
  • 22
  • 1
    Please describe what you are actually trying to do, i.e. what problem are you trying to solve? To me it sounds like using a textfile is a really poor approach for some kind of communication between two processes. You might be way better off using a database or some other tool/protocol to achieve your goal – NoConnection Aug 09 '22 at 12:00
  • 1
    use a named system mutex to lock process: https://stackoverflow.com/questions/3639138/cross-process-locking-in-c-sharp | https://stackoverflow.com/questions/30205648/how-to-have-processes-not-threads-in-c-sharp-synchronize-file-system-access | https://stackoverflow.com/questions/1795143/synchronization-between-two-processes-in-c – Rand Random Aug 09 '22 at 12:08
  • `But, we cannot close the FileStream in parent because it is keep on adding the data.` - you will have to close the filestream at some point, even if it is for a short period of time, can't have both write at the same time, one needs to finish its job so the other can do its job - if both could write at the same time, you will end up with a race condition and data could get lost – Rand Random Aug 09 '22 at 12:11
  • 1
    It is your own "child" process that keeps the file locked. You have opened the stream but then you don't use that stream. File.WriteAllText will try to open itself the file clashing with your own opened stream – Steve Aug 09 '22 at 12:12
  • 1
    @Steve - didn't see that, you have some sharp eyes :) – Rand Random Aug 09 '22 at 12:14
  • 1
    @RandRandom Let's call it experience with these problems. – Steve Aug 09 '22 at 12:15
  • @RandRandom I'm using "Using" Block. I thought it will do the same. Am I wrong? And I didn't add any fs.close() – Chanikya Aug 09 '22 at 12:30
  • nothing wrong with your code about closing, its just you claimed this statement: `But, we cannot close the FileStream in parent because it is keep on adding the data.` - so, I wasn't refering to your code, but simply what you are saying - and yes, using is the way to go, manually calling close isn't necessary – Rand Random Aug 09 '22 at 12:32

0 Answers0