문제

i have made a userControl library .. and it contains NetworkStream, StreamReader, FileStream
So do i have to Dispose them all when the form that has this userControl closes ??

I mean There's no such Form1_FormClosing(object sender,FormClosingEventArgs e) in a userControl so when should i dispose those streams?
Does userControl1.Dispose() take care of that?

thanks in advance :)

도움이 되었습니까?

해결책

UserControl.Dispose() disposes the components in it's Controls collection, but nothing more.

You can handle the UserControl.Disposed event, or you can properly implement the Dispose pattern.

For C# user controls, protected override void Dispose(bool disposing) is auto-created in UserControl1.Designer.cs. You can amend it to:

protected override void Dispose(bool disposing)
{
    if (disposing)
    {
        if (components != null)
        {
            components.Dispose();
        }

        // Dispose your streams here
    }

    base.Dispose(disposing);
}

다른 팁

userControl1.Dispose() does not magically take care of it unless you override it and put in code to dispose your objects, which is what you should do.

Have a look at this answer to see how to implement it, replace the event de-regestering with your calls to dispose your streams.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top