【发布时间】:2010-01-14 22:06:45
【问题描述】:
我有一个秒表在不同的线程中运行,它会更新标签中的 GUI 线程以随着时间的推移显示。当我的程序关闭时,当我在表单 GUI 中调用 this.Invoke(mydelegate); 以使用秒表中的时间更新标签时,它会抛出 ObjectDisposedException。
如何摆脱这个ObjectDisposedException?
我试图在 FormClosing 事件中停止秒表,但它没有处理它。
代码如下:
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
stopwatch = sw;
sw.Start();
//System.Threading.Thread.Sleep(100);
System.Threading.Thread t = new System.Threading.Thread(delegate()
{
while (true)
{
TimeSpan ts = sw.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
timeElapse = elapsedTime;
UpdateLabel();
}
});
stopwatchThread = t;
t.Start();
public void UpdateLabel()
{
db = new doupdate(DoUpdateLabel);
this.Invoke(db);
}
public void DoUpdateLabel()
{
toolStripStatusLabel1.Text = timeElapse;
}
【问题讨论】:
标签: c# multithreading timer stopwatch objectdisposedexception