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事件处理程序中,我正在创建一个取消标记源,并在各种异步调用中使用相关的令牌。
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 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.
我想知道在哪里做这个。如果有一个卸载事件我可以捕获,那么那将是完美的-但没有。事实上,我找不到任何合适的活动。
I could trap the close event for the containing Form, but I really wanted to keep everything local to my UserControl.
我可以捕获包含表单的close事件,但我确实希望将所有内容都保留到UserControl中。
Suggestions?
建议吗?
4 个解决方案
#1
31
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.
我建议控件:HandleDestroyed事件。当底层HWnd被破坏时(通常在父窗体关闭时发生),它被引发。要在自己的UserControl中处理它,应该覆盖onhandledestroy。
You have full access to the Control's properties at this moment, because it is not yet disposed of.
此时您可以完全访问控件的属性,因为它还没有被处理。
#2
10
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
}
#3
1
Why not just use the Disposed
event?
为什么不使用已处理的事件呢?
When a form is closing, it will call Dispose
on itself and all child controls will be disposed recursively as well.
当窗体关闭时,它将对自身调用Dispose,所有子控件也将被递归地处理。
#4
0
Try this:
试试这个:
UserControl.Dispose();
#1
31
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.
我建议控件:HandleDestroyed事件。当底层HWnd被破坏时(通常在父窗体关闭时发生),它被引发。要在自己的UserControl中处理它,应该覆盖onhandledestroy。
You have full access to the Control's properties at this moment, because it is not yet disposed of.
此时您可以完全访问控件的属性,因为它还没有被处理。
#2
10
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
}
#3
1
Why not just use the Disposed
event?
为什么不使用已处理的事件呢?
When a form is closing, it will call Dispose
on itself and all child controls will be disposed recursively as well.
当窗体关闭时,它将对自身调用Dispose,所有子控件也将被递归地处理。
#4
0
Try this:
试试这个:
UserControl.Dispose();