简体   繁体   English

在表单上处理ActiveX控件

[英]Disposing ActiveX Control on a Form

I have a third party ActiveX control that I am embedding in a form. 我有一个第三方ActiveX控件,我嵌入在一个表单中。 The control's class implements IDisposable. 控件的类实现了IDisposable。 My question is should I manually call the control's dispose method either in the form closing event or go into the designer and edit the form dispose method to call the control's dispose. 我的问题是我应该在表单关闭事件中手动调用控件的dispose方法,还是进入设计器并编辑表单dispose方法来调用控件的dispose。

I understand that a form calls the dispose methods of all the controls it owns, however are there any exceptions since this is a COM control. 我知道一个表单调用它拥有的所有控件的dispose方法,但是有任何异常,因为这是一个COM控件。 Thanks. 谢谢。

PS: I am using Visual Studio 2008, .Net 3.5, Project Type: Windows Forms Application PS:我使用的是Visual Studio 2008,.Net 3.5,项目类型:Windows Forms Application

MSDN Marshal.ReleaseComObject In order to Release or Free a COM Object you need to use this code MSDN Marshal.ReleaseComObject为了释放或释放COM对象,您需要使用此代码

System.Runtime.InteropServices.Marshal.ReleaseComObject( someComObject); 

//this is your COM Object Dispose will not work on a COM Object for example Debug and try to Dispose your object using code like this if you have an object named fs //这是你的COM对象Dispose将无法在COM对象上工作,例如Debug,如果有一个名为fs的对象,尝试使用这样的代码来处理你的对象

((IDisposable)fs).Dispose();

No, there is nothing special you have to do. 不,你不需要做什么特别的事。

Because the Control class (from which other container controls such as Form inherit from) implements the IDisposable interface , it should (if it follows the standard IDisposable implementation guidelines ) dispose of any references that it holds that implement the IDisposable interface. 因为Control (其他容器控件,例如Form继承自)实现了IDisposable接口 ,所以它应该(如果它遵循标准的IDisposable实现指南 )处理它所拥有的实现IDisposable接口的任何引用。

That said, your control, assuming that it is in the controls collection exposed by the Controls property , will have it's IDisposable.Dispose method called when the window containing it is closed (as closing the window causes the window's Dispose method to be called). 也就是说,您的控件(假设它位于Controls属性公开的控件集合中)将在包含它的窗口关闭时调用它的IDisposable.Dispose方法 (因为关闭窗口会导致调用窗口的Dispose方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM