简体   繁体   English

处置后事件处理程序停止工作

[英]Event Handler Stops Working After Dispose

The three ImageButtons below are added to a panel (pBreakfast) in a page's codebehind: 下面的三个ImageButton在页面代码的后面添加到了面板(pBreakfast):

ImageButton ibSR = new ImageButton();
ibSR.ID = "ibStickyRoll";
ibSR.ImageUrl = "/images/Breakfast.gif";
ibSR.Click += new ImageClickEventHandler(ImageButton_Click);
pBreakfast.Controls.Add(ibSR);

ImageButton ibD = new ImageButton();
ibD.ID = "ibDoughnut";
ibD.ImageUrl = "/images/Breakfast.gif";
ibD.Click += new ImageClickEventHandler(ImageButton_Click);
pBreakfast.Controls.Add(ibD);
ibD.Dispose();

using(ImageButton ibC = new ImageButton()){
   ibC.ID = "ibCrepe";
   ibC.ImageUrl = "/images/Breakfast.gif";
   ibC.Click += new ImageClickEventHandler(ImageButton_Click);
   pBreakfast.Controls.Add(ibC);
}

I expected either all three would work, or perhaps the disposed ImageButtons would generate an error or simply not appear. 我希望这三个都可以使用,或者放置的ImageButtons会产生错误或根本不会出现。 But what's happening is all three appear and don't cause any errors, but the event handler never fires for the ImageButtons that are disposed. 但是发生的事情是这三个都出现了,并且不会引起任何错误,但是事件处理程序永远不会为所处理的ImageButton触发。

Why does disposing cause only the eventhandler hookup to go away? 为什么配置仅导致事件处理程序连接消失?

I am dynamically adding TableRow's and TableCell's to a table on this same page and placing ImageButtons in one cell of each row. 我正在将TableRow和TableCell动态添加到同一页上的表中,并将ImageButtons放置在每一行的一个单元格中。 I am using 'using' with the TableRow's and TableCell's without any problem. 我正在使用TableRow和TableCell的“使用”,没有任何问题。 There doesn't seem to be an issue with the outer (TableRow & TableCell) objects getting disposed; 外部(TableRow&TableCell)对象被处置似乎没有问题; as long as I don't ever dispose of the dynamic ImageButtons, the eventhandler gets fired OK on click. 只要我从不丢弃动态ImageButton,事件处理程序就会在单击后被触发。

Is it OK, in this instance, to not ever dispose of the ImageButtons? 在这种情况下,永远不要丢弃ImageButtons可以吗? I have taken StackOverflow's advice to heart and try to use using() on all my disposable objects -- so this is really bugging me. 我牢记了StackOverflow的建议,并尝试在所有一次性对象上使用using()-因此,这确实困扰了我。

Thanks! 谢谢!

Only Dispose an object after everything is completely done with it. 仅在完全完成对象处理后再对其进行处置。 Trying to do anything with the object after it is disposed will result in undefined behavior. 处置对象后尝试对其执行任何操作将导致不确定的行为。 Think of it as freeing the memory. 将其视为释放内存。

Fortunately, Windows forms disposes these objects for you when you close the form. 幸运的是,Windows窗体在您关闭窗体时会为您处理这些对象。 The only case you would need to dispose them in your code is if you remove the objects from the form. 您唯一需要在代码中处理它们的情况是从表单中删除对象。

Take a look at the .Designer.cs file, and you will see the form's Dispose() method: 看一下.Designer.cs文件,您将看到表单的Dispose()方法:

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
    if (disposing && (components != null))
    {
        components.Dispose();
    }
    base.Dispose(disposing);
}

The components.Dispose() will recursively clean up all the components on the form, including those in the panel. components.Dispose()将递归清除表单上的所有组件,包括面板中的组件。

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

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