我有一個運行在不同線程中的秒錶,它更新了標籤中的GUI線程以隨時間推移顯示。當我的程序關閉時,如果我在表單GUI中調用this.Invoke(mydelegate);
來更新帶有秒錶時間的標籤,它會拋出ObjectDisposedException
。ObjectDisposedException - 在GUI線程中運行秒錶
我該如何擺脫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;
}
你可以發佈代碼嗎? – hackerhasid 2010-01-14 22:11:38