什么事件表示UserControl被销毁? [英] What event signals that a UserControl is being destroyed?

查看:319
本文介绍了什么事件表示UserControl被销毁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

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.

我正在尝试找出位置来执行此操作.如果有一个我可以捕获的Unload事件,那将是完美的-但事实并非如此.实际上,我找不到任何合适的事件.

I'm trying to figure out where to 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.

可以捕获包含表单的关闭事件,但是我真的很想将所有内容都保留在UserControl的本地.

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

建议?

推荐答案

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

I suggest the Control::HandleDestroyed event. 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的属性,因为尚未处理.

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

这篇关于什么事件表示UserControl被销毁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆