English 中文(简体)
4. BinaryReader或撰写人。 关闭C#
原标题:BinaryReader or Writer.Close() is not closing properly C#

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; }
            }
        }
最佳回答

您是否知道<代码>continue。 关键词是什么? 请注意,接下来的loop,而不是下一个block of Code。 因此,如果出现封闭式<代码>EnvBinRead的情况,将not进入封闭式<代码>EnvBinWrite的栏号,但下一个项目则继续使用<编码>AllBldgs。

为了消除所有例外,并且仍然试图关闭所有双亲作家,你写道:

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());
    }
}

请注意,仅吃紧的是never。 a 良好想法。 如果你不注意读者或写字是否可以关闭,则检查该读物是否按照评论中的建议在结束之前就已启动。

问题回答

您应呼吁处置者关闭本安提尔和撰稿人。

<>Explanation:

<代码>StreamReader、BigaryReader和BinaryWriter,在你打电话给他们时,所有近距离/处置其下流。 如果读者/作者只是收集的垃圾,他们就不处置溪流——你总是处置读者/作者,更喜欢使用<<>条/代码>的说明。 (事实上,这些班级都没有定级人,也不应该有)

Personally I prefer to have a using statement for the stream as well. You can nest using statements without braces quite neatly:

using (Stream stream = ...)
using (StreamReader reader = new StreamReader(stream, Encoding.Whatever))
{
}

即便在“<条码>上>使用对流的表述有些多余(无<代码>)。 页: 1 如果你删除了<代码>,我认为这是最佳的做法。 精简,并在稍后日期直接使用该流,你已经拥有适当的处置特性。

It is always good to use Using block for the streams, Closing them immediately after use.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签