title: Need to close filestreams that were opened in another context of the same app
body: <p>I have a main window on which a user clicks a button to process a selected input file. The block of code triggered by the button opens the input file, an output file, and a log file. That same block of code reads lines of the input file one at a time, does some operations on the fields in each line, then writes a corresponding line to the output file. Error messages are written to the log file. Processing a single file can take minutes.</p>
  
  <p>I want to be able to gracefully close the 3 files if the user gets impatient and closes the main window using the red "X" button. I have an event handler that captures the clicking of the "X", but I am unable to reference the 3 filestreams I want to close, because they were created outside of the event handler's context. How do I pass to the handler the 3 filestreams?</p>
  
  <pre><code>namespace less
  {
     public partial class MainWindow : Window
     {
          ...
  
          private static string filename;
  
          ...
  
        public MainWindow()
        {
  
              InitializeComponent();
  
              // string filename is chosen in this block of code
  
        }
  
      private void convertData_Click(object sender, RoutedEventArgs e)
          {
              // This is invoked when user clicks the "Process Data" button
  
              System.IO.StreamReader inputFile = new System.IO.StreamReader(filename);
              System.IO.StreamWriter outputFile = new System.IO.StreamWriter(filename + ".less.csv");
              System.IO.StreamWriter logFile = new System.IO.StreamWriter(filename + ".log");
  
                       ....
  
                 &lt;process all the data&gt;
  
                       ....
  
             inputFile.Close();  // &lt;- this works
             outputFile.Close();  // &lt;- this works
             logFile.Close();  // &lt;- this works
  
             this.Close();
           }
  
      private void MainWindow_Closed(object sender, EventArgs e)
          {
  
             // This is the event handler invoked when the red "X" is clicked.
  
             inputFile.Close(); // &lt;- this (and following) do not work
             outputFile.Close();
             logFile.Close();
          }
  
     } // closes public partial class MainWindow : Window
  
  } // closes namespace
  </code></pre>