1

시간이 경과함에 따라 표시 할 레이블의 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; 
     } 
+0

코드를 게시 할 수 있습니까? – hackerhasid

답변

2

:

여기에 코드입니다. FormClosing 이벤트에서 응용 프로그램을 닫기 전에 스레드를 먼저 멈출 수 있습니까? 지금 BackgroundWorker에와 배경 스레드 때까지 닫지 않는 양식을 보장하는 순서대로 종료를 사용

+2

또한 이것을 위해 BackgroundWorker를 사용하는 것이 좋습니다. 이 문제가 해결되어 기쁩니다 – SwDevMan81

2

같은 코드가 먼저 실행이 중지되었습니다 :

using System; 
using System.Threading; 
using System.Windows.Forms; 
using System.Diagnostics; 

namespace WindowsFormsApplication1 { 
    public partial class Form1 : Form { 
     public Form1() { 
      InitializeComponent(); 
      backgroundWorker1.DoWork += backgroundWorker1_DoWork; 
      backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged; 
      backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted; 
      backgroundWorker1.WorkerReportsProgress = backgroundWorker1.WorkerSupportsCancellation = true; 
      backgroundWorker1.RunWorkerAsync(); 
     } 
     bool mCancel; 
     void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e) { 
      if (e.Error != null) MessageBox.Show(e.Error.ToString()); 
      if (mCancel) this.Close(); 
     } 
     protected override void OnFormClosing(FormClosingEventArgs e) { 
      if (backgroundWorker1.IsBusy) mCancel = e.Cancel = true; 
      backgroundWorker1.CancelAsync(); 
     } 
     void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e) { 
      label1.Text = e.UserState as string; 
     } 
     void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { 
      Stopwatch sw = Stopwatch.StartNew(); 
      while (!backgroundWorker1.CancellationPending) { 
       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); 
       backgroundWorker1.ReportProgress(0, elapsedTime); 
       Thread.Sleep(15); 
      } 
     } 
    } 
} 

참고 슬립() 호출이 그것을 필요하다고 초당 약 1000 번 이상 UI 스레드에 대한 호출을 마샬링 할 수 없습니다.

+0

+1 아주 좋은 !!! 그 점에 대해 감사드립니다 !!! :) –