gpt4 book ai didi

c# - 处理订阅事件的类

转载 作者:太空宇宙 更新时间:2023-11-03 14:05:07 25 4
gpt4 key购买 nike

我有一个像这样的 System.Windows.Forms.Form:

public class MainForm : Form
{
int _processProgress;
public int ProcessProgress
{
get { return _processProgress; }
set
{
_processProgress = value;
if (ProcessProgressChanged != null)
ProcessProgressChanged(value);
}
}

public delegate void ProcessProgressChangedEventHandler(int progressPercentage);
public event ProcessProgressChangedEventHandler ProcessProgressChanged;
}

它有一个像这样的 UserControl:

public class MainFormControl : UserControl
{
public MainFormControl()
{
((MainForm)this.ParentForm).ProcessProgressChanged += (progPerc) =>
{
this.TextBox1.Text = "asd";
// Do something
};
}
}

它会从 MainFormControl MainForm 的构造函数中取消订阅匿名方法吗? ProcessProgressChanged MainFormControl.Dispose() 被调用时的事件(或者当 MainFormControl 从 MainForm 中移除时) ?

我的代码在 C# 中,框架 4,在 VS2010 Pro 中构建,项目在 WinForms 中。

请帮忙。提前致谢。

最佳答案

没有。但是如果控件在表单上并且正在处理表单,那真的没关系......

您必须注意的是表单/控件何时连接到长期运行的后端服务(单例等)。您必须注意那里,因为事件可以/将触发,即使控件/表单被处置。如果处理程序做了一些假设 UI 仍然存在(即未处理)的事情——你就会遇到麻烦......

关于c# - 处理订阅事件的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9527177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com