简体   繁体   中英

Using TextWriter with StreamWriter and Reading/Writing Simultaneously

As the title suggests, I'm attempting to read and write to a file at the same time. I have researched this topic but the answers I have found don't seem to work for me because of the circumstances in my program. I am using multiple FileSystemWatchers to track a large amount of files that are constantly passing through a flow in my network. As the files pass through each part of my flow, a text file is updated(one text file per spot in the flow) that marks the name of the file and the time it was created within the folder. It is unpredictable when files might be passing through, and when they might be writing to the tracker text files. My goal is to be able to read and write to the file simultaneously, in case a user attempts to read to from a text file that is being written to at the exact same time. How would I accomplish this?

//Write to File
    private void WriteToFile(string info,string path,string tr)
    {
        if (!File.Exists(path+@"\"+@tr))
        {
            var myFile =
            File.Create(path + @"\" + @tr);
            myFile.Close();
            TextWriter tw = new StreamWriter(path + @"\" + @tr, true);
            tw.WriteLine(info,true);
            tw.Close();
        }
        else if (File.Exists(path + @"\" + @tr))
        {
            TextWriter tw = new StreamWriter(path + @"\" + @tr, true);
            tw.WriteLine(info);
            tw.Close();
        }
    }

The circumstances you are alluding to seem to say that while multiple attempts can be made to read/write the file at a given time, you still want to ensure that the operations are performed one after another in the correct order that the read or writes got called.

One simple method of ensuring that the read and write operations are synchronized would be to just put a lock or Monitor around the methods. Try the following code for your write method:

private readonly object _locker = new object();

// write the file
private void WriteToFile(string info, string path, string tr)
{
    Monitor.Enter(this._locker);

    try
    {
        if (!File.Exists(path + @"\" + @tr))
        {
            var myFile =
            File.Create(path + @"\" + @tr);
            myFile.Close();
            TextWriter tw = new StreamWriter(path + @"\" + @tr, true);
            tw.WriteLine(info, true);
            tw.Close();
        }
        else if (File.Exists(path + @"\" + @tr))
        {
            TextWriter tw = new StreamWriter(path + @"\" + @tr, true);
            tw.WriteLine(info);
            tw.Close();
        }
    }
    finally
    {
        Monitor.Exit(this._locker);
    }
}

Then, I would use a very similar construct for reading the file.

// read the file
private string ReadFile(string path)
{
    Monitor.Enter(this._locker);

    try
    {
        // read the file here...
    }
    finally
    {
        Monitor.Exit(this._locker);
    }
}

What the Monitor will do is ensure that the file will not be read until an on-going write operation is complete (and vice-versa). This will ensure that you will not get the old data when you read it, and you will also not over-write the new data (which has not yet been read). This method verifies the integrity of your files at all times.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM