C# 什么事件表明 UserControl 正在被销毁?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12474566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:22:44  来源:igfitidea点击:

What event signals that a UserControl is being destroyed?

c#.netwinformseventsasynchronous

提问by Gary McGill

I have a UserControl-derived control the displays some information fetched from a web server. I'm currently in the process of making the initialization of the control asyncronous, to improve responsiveness.

我有一个 UserControl 派生的控件,它显示从 Web 服务器获取的一些信息。我目前正在使控件的初始化异步,以提高响应能力。

In my Load event handler, I'm creating a CancellationTokenSource, and using the associated Token in the various async calls.

在我的 Load 事件处理程序中,我正在创建一个 CancellationTokenSource,并在各种异步调用中使用关联的 Token。

I now want to ensure that if the user closes the form before the async operation completes, the operation will be cancelled. In other words, I want to call Cancel on the token.

我现在想确保如果用户在异步操作完成之前关闭表单,操作将被取消。换句话说,我想在令牌上调用 Cancel。

I'm trying to figure out whereto do this. If there was an Unload event that I could trap, then that would be perfect - but there isn't. In fact, I can't find any event that looks suitable.

我想弄清楚在哪里做这件事。如果有一个我可以捕获的 Unload 事件,那将是完美的 - 但没有。事实上,我找不到任何看起来合适的事件。

I couldtrap the close event for the containing Form, but I really wanted to keep everything local to my UserControl.

可以捕获包含 Form 的 close 事件,但我真的希望将所有内容保留在我的 UserControl 本地。

Suggestions?

建议?

采纳答案by Stephan

I suggest the Control::HandleDestroyedevent. It is raised, when the underlying HWnd is destroyed (which usually happens, when the parent form is closed). To handle it in your own UserControl, you should override OnHandleDestroyed.

我建议使用Control::HandleDestroyed事件。当底层 HWnd 被销毁时(通常发生在父窗体关闭时),它会被引发。要在您自己的 UserControl 中处理它,您应该覆盖OnHandleDestroyed

You have full access to the Control's properties at this moment, because it is not yet disposed of.

此时您可以完全访问 Control 的属性,因为它还没有被处理掉。

回答by sloth

Why not just use the Disposedevent?

为什么不直接使用Disposed事件?

When a form is closing, it will call Disposeon itself and all child controls will be disposed recursively as well.

当一个窗体关闭时,它会调用Dispose自己,所有子控件也将被递归处理。

回答by Gimhan

Try this:

尝试这个:

UserControl.Dispose();

回答by Avram

Another solution

另一种解决方案

    protected override void OnParentChanged(EventArgs e)
    {
        base.OnParentChanged(e);

        if (parentForm != null)
        {
            parentForm.Closing -= parentForm_Closing;
        }
        parentForm = FindForm();

        if (parentForm != null)
            parentForm.Closing += parentForm_Closing;
    }

    void parentForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        parentForm.Closing -= parentForm_Closing;
        parentForm = null;
        //closing code
    }