开发者

Do I have to call Dispose method on FolderBrowserDialog?

The FolderBrowserDialog component in .NET Framework (and the OpenFileDialog) implements开发者_运维百科 the IDisposible interface, meaning we should call its Dispose method at some appropriate time after we've done with it or something bad happens (unmanaged resource leaks).

In Visual Studio WinForm designer, if I drag a FolderBrowserDialog component onto a Form, the code generated by the designer does not seem to take care of this at all, no code calls the Dispose method on the FolderBrowserDialog.

In contrast, if I drag a Timer (the one in the System.Windows.Forms namespace) which also implements IDisposible interface, the code generated would be:

this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);

By associating the timer with the container (this.components), the timer is guaranteed to be properly disposed when the container is disposed- happens when Form.Close() or Form.Dispose() is called.

So why FolderBrowserDialog component receives this special treatment?


Good spot! The reason is probably that the FolderBrowserDialog does not provide a constructor that takes an IContainer argument, whereas Timer does.

As to why this is so, you can only ask the original winforms designers. Maybe it isn't really designed to be used in the designer in this way? They only meant it to be used in code in a using statement?

Bear in mind that FolderBrowserDialog, and its parents, don't actually override Dispose from Component, so it doesn't actually need to dispose anything.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜