Closing a file without using

I have a class that reads data from one file stream and writes it to another. I am worried about closing threads after processing in closeFiles ().

How would you deal with the possibility that disposing of one thread may throw an exception that stops calling another thread from the called one.

Should I call closely and manage streams or just one?

What happens if I catch any errors from the stream and then continue moving and deleting files, as shown in lastOperation ()?

In an ideal world, I would like to use the using statement in a C ++ style initialization list, but I'm sure this is not possible in C #.

EDIT : thanks for the quick replies answers. So, what should I do is infer from IDisposable, and then change the constructor and add two recycling methods as follows:

    ~FileProcessor()
    {
        Dispose(true);
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
    private void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                sw.Flush();
            }
            closeFiles();
            disposed = true;
        }
    }

This is basically what I am doing:

class FileProcessor
{
    private string in_filename;
    private string out_filename;
    private StreamReader sr;
    private StreamWriter sw;
    bool filesOpen = false;

    public FileProcessor(string filename)
    {
        in_filename = filename; 
        out_filename = filename + ".out";
        openFiles();
    }

    ~FileProcessor()
    {
        closeFiles();
    }

    private void openFiles()
    {
        sr = new StreamReader(in_filename);
        sw = new StreamWriter(out_filename);
        filesOpen = true;
    }

    private void closeFiles()
    {
        if (filesOpen)
        {
            sr.Close();
            sw.Close();
            sr.Dispose();
            sw.Dispose();
            filesOpen = false;
        }
    }

    /* various functions to read, process and write to the files */

    public void lastOperation()
    {
        closeFiles();
        File.Delete( in_filename );
        Directory.Move(out_filename, outdir + out_filename);
    }
}
+3
source share
4 answers

There should not be a destructor in your FileProcessor class. It is useless, but it is expensive.

It must have Dispose()(and implement the IDisposable interface) to call closeFiles ().

And as @marcelo answered, Stream.Dispose () should not throw. You can rely on this for BCL classes.

But you should check each Reader / Writer for null, in case the first one was open, but the second one failed:

if (sr != null) sr.Dispose();
if (sw != null) sw.Dispose();

filesOpen .

+3

, IDisposable, IDisposable-.

, Dispose() . , , , .

+3

Dispose . .

+2

# using. , using, IDisposable. Dispose, .

If both your StreamReader and StreamWriter implement IDisposable, you can put them in the use block and they will be deleted when you are done with them.

using(var sr = new StreamReader(in_filename)) {
    // Perform reader actions
}
// Reader will now be disposed.
0
source

Source: https://habr.com/ru/post/1777422/


All Articles