Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7918793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:37:40+00:00 2026-06-03T15:37:40+00:00

I have a form application that performs a simulation and constantly reads/writes a binary

  • 0

I have a form application that performs a simulation and constantly reads/writes a binary file. Everything works fine if you let it run through. However, if the form is closed/ the simulation is aborted the file-stream is not closed properly – leaving the file locked.
Is there a way to make sure all streams closed? I tried the following – but it has no effect…
Thanks a lot in advance,
T

        public BinaryWriter BinWrite;
        public BinaryReader BinRead;

        public BinaryWriter EnvBinWrite;
        public BinaryReader EnvBinRead;       

public void theForm_FormClosing(object sender, FormClosingEventArgs e)
        {

            //Close all binary file reader/writers -- crashes if it cannot overwrite files
            foreach (Building B in AllBldgs)
            {
                try
                {
                    EnvBinRead.Close();
                }
                catch
                { continue; }
                try
                {
                    EnvBinWrite.Close();
                }
                catch
                { continue; }
                try
                {
                    BinRead.Close();
                }
                catch
                { continue; }

                try
                {
                    BinWrite.Close();
                }
                catch
                { continue; }
            }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T15:37:42+00:00Added an answer on June 3, 2026 at 3:37 pm

    Are you sure you know what the continue keyword is for? Please note that this continues with the next loop, not the next block of code. So if an exception occurs closing EnvBinRead, you will not enter the block to close EnvBinWrite, but continue with the next item from AllBldgs.

    To eat all exceptions and still try to close all the binary writers, you’d write:

    foreach (Building B in AllBldgs)
    {
        try
        {
            EnvBinRead.Close();
        }
        catch (Exception exp)
        { 
            Console.WriteLine("Closing EnvBinRead failed!" + exp.ToString());
        }
    
        try
        {
            EnvBinWrite.Close();
        }
        catch (Exception exp)
        { 
            Console.WriteLine("Closing EnvBinWrite failed!" + exp.ToString());
        }
    
        try
        {
            BinRead.Close();
        }
        catch (Exception exp)
        { 
            Console.WriteLine("Closing BinRead failed!" + exp.ToString());
        }
    
        try
        {
            BinWrite.Close();
        }
        catch (Exception exp)
        { 
            Console.WriteLine("Closing BinWrite failed!" + exp.ToString());
        }
    }
    

    Please note that simply eating the exceptions is never a good idea. If you don’t care whether the reader or write could be closed, check whether it has been initialized before closing it as suggested in the comments.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an C# form application that use an access database. This application works
I have a Windows form application that's consuming web services over https and is
I have a C# Windows Form application that contains a menu with this event:
I have a combo box in a C# Windows form application that is being
I have one form in a PHP (5.2.9-1) application that causes IIS (Microsoft-IIS/6.0) to
I have a Winforms application that dynamically instantiates external form objects for use in
I have an application that consists of two forms. One form displays data returned
I have a Delphi application that has a document browser as the main form.
I have a Windows application that has 2 Forms . From one form I
I have application in VB.net that have two different form (Form1 and Form2). Now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.