c# - 关闭应用程序时如何避免 ObjectDisposedException
问题描述
我在整个应用程序中都复制了一个问题,但我认为它可以以相同的方式进行管理。
我的 Winforms 应用程序中有一个 IDisposable 类(它是从 System.Windows.Forms.Label 派生的自定义控件)
在那个类中,我有一个计时器,其滴答事件是这样的:
private void TickTimer(object state)
{
Action updateText = () =>
{
this.Parent.SuspendLayout();
this.Text = DateTime.Now.ToString("HH:mm:ss");
this.Parent.ResumeLayout(false);
};
if (this.InvokeRequired)
this.Invoke(updateText);
else
updateText();
}
另一方面,这是 Dispose 方法:
protected override void Dispose(bool disposing)
{
if (disposing)
{
Stop();
_timer.Dispose();
}
base.Dispose(disposing);
}
问题是在关闭应用程序时,在 this.Invoke(updateText) 调用中抛出异常,告诉主窗体(放置控件的位置)已被释放。
由于这是异步发生的,我该如何管理呢?
我曾考虑在 TickTimer 的每一行中放置一个名为 _isDisposed 的类字段,以检查它是否已被释放,但这真的很难看。
通过在 StackOverflow 中搜索,我找到了一些建议……没有一个奏效。最后的尝试是使用“更新”标志。
private void TickTimer(object state)
{
_updating = true;
Action updateText = () =>
{
this.Parent.SuspendLayout();
this.Text = DateTime.Now.ToString("HH:mm:ss");
this.Parent.ResumeLayout(false);
_updating = false;
};
if (this.InvokeRequired)
this.Invoke(updateText);
else
updateText();
}
在 Dispose 方法中:
protected override void Dispose(bool disposing)
{
if (disposing)
{
Stop();
_timer.Dispose();
while (_updating) ;
}
base.Dispose(disposing);
}
但是会发生同样的错误。似乎父窗体被放置在子控件之前。
最好的方法是什么?
海梅
解决方案
推荐阅读
- overlay - SpatRaster 和使用 terra 包的向量之间的数学运算
- django - Djngo Signals 如何在查询集中找到重复项并将它们指定为发送方和接收方?
- sql - IllegalArgumentException:属性 PublicID 不在根类型 entity.UserRegion 上
- python - 更改QSpinBox中upbutton和downbutton的背景颜色
- python - 无法从heroku调用api
- python - 为什么二进制精度与 Keras 中的 train_on_batch 二进制精度不匹配?
- scala - Akka-Http 是否支持请求范围状态/隐式?
- c++ - c ++结构中的多个lambda?
- typescript - TypeScript - 如果两种类型相交,如何抛出编译时错误?
- google-data-studio - 过滤器只过滤来自一个数据源谷歌数据工作室的数据