2013-03-21 2 views
0

GUI를 매 초마다 업데이트하려고합니다. 나는 System.Timers.Timer, System.Windows.Forms.Timer 및 BackgroundWorker를 시도했다. 주사위가 없다. 나는 분명히 뭔가를 놓친다. BackgroundWorker에와GUI가 타이머 또는 BackgroundWorker로 업데이트되지 않습니다.

내 시도 :;`실패의 원인에 대한 강력한 후보처럼 보이는

public partial class Form1 : Form 
{ 
    List<int> counts = new List<int>() { 0, 0, 0, 0, 0, 0, 0, 0 }; 
    List<long> complete = new List<long>() { 0, 0, 0, 0, 0, 0, 0, 0 }; 


    private void button1_Click(object sender, EventArgs e) 
    { 
     var backgroundWorker = new BackgroundWorker(); 
     backgroundWorker.DoWork += new DoWorkEventHandler(UpdateGUI); 
     backgroundWorker.RunWorkerAsync(); 

     // Do stuff that updates counts and complete 

     backgroundWorker.CancelAsync(); 
    } 

    private void UpdateGUI(object sender, EventArgs e) 
    { 
     var backgroundWorker = sender as BackgroundWorker; 
     while (backgroundWorker.CancellationPending == false) 
     { 

      label1.Text = count.ToString(); 
      labelF1.Text = counts[0].ToString(); 
      labelF2.Text = counts[1].ToString(); 
      labelF3.Text = counts[2].ToString(); 
      labelF4.Text = counts[3].ToString(); 
      labelF5.Text = counts[4].ToString(); 
      labelF6.Text = counts[5].ToString(); 
      labelF7.Text = counts[6].ToString(); 
      labelF8.Text = counts[7].ToString(); 


      labelC1.Text = complete[0].ToString(); 
      labelC2.Text = complete[1].ToString(); 
      labelC3.Text = complete[2].ToString(); 
      labelC4.Text = complete[3].ToString(); 
      labelC5.Text = complete[4].ToString(); 
      labelC6.Text = complete[5].ToString(); 
      labelC7.Text = complete[6].ToString(); 
      labelC8.Text = complete[7].ToString(); 

      Application.DoEvents(); 

      Thread.Sleep(1000); 
     } 
    } 
} 
+0

이 –

+0

가'backgroundWorker.CancelAsync() 예외를지고 당신을 했나 봅니다. 또한 데이터가 변경되지 않는 것이 문제가 될 수도 있습니다. 또한 잠글 생각없이 공유 상태를 읽고 쓰고 있습니다. 결국 "업데이트하지 않음"은 문제에 대한 설명이 매우 형편 없기 때문에 여기서 언급 한 내용은 추측 일뿐입니다. 이 콜백은 UI 스레드에서 발생하도록 보장되어 있으므로 System.Windows.Forms.Timer를 계속 사용합니다. 'Application.DoEvents'는 모든 앱에 반드시 필요하지는 않습니다. 전화해야하는 경우, 이미 잘못했기 때문입니다. – spender

+0

@spender "Count counts and complete"섹션에서 UpDateGUI()를 호출하면 모두 정상입니다. –

답변

0

public partial class Form1 : Form 
{ 
List<int> counts = new List<int>() { 0, 0, 0, 0, 0, 0, 0, 0 }; 
List<long> complete = new List<long>() { 0, 0, 0, 0, 0, 0, 0, 0 }; 


private void button1_Click(object sender, EventArgs e) 
{ 
    var backgroundWorker = new BackgroundWorker(); 
    backgroundWorker.DoWork += new DoWorkEventHandler(UpdateGUI); 
    backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted 
    backgroundWorker.RunWorkerAsync(); 

    // Do stuff that updates counts and complete 

    //backgroundWorker.CancelAsync(); 
} 

private void UpdateGUI(object sender, EventArgs e) 
{ 

     Thread.Sleep(1000); 

} 
private void backgroundWorker.RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
    { 
    var backgroundWorker = sender as BackgroundWorker; 

     label1.Text = count.ToString(); 
     labelF1.Text = counts[0].ToString(); 
     labelF2.Text = counts[1].ToString(); 
     labelF3.Text = counts[2].ToString(); 
     labelF4.Text = counts[3].ToString(); 
     labelF5.Text = counts[4].ToString(); 
     labelF6.Text = counts[5].ToString(); 
     labelF7.Text = counts[6].ToString(); 
     labelF8.Text = counts[7].ToString(); 


     labelC1.Text = complete[0].ToString(); 
     labelC2.Text = complete[1].ToString(); 
     labelC3.Text = complete[2].ToString(); 
     labelC4.Text = complete[3].ToString(); 
     labelC5.Text = complete[4].ToString(); 
     labelC6.Text = complete[5].ToString(); 
     labelC7.Text = complete[6].ToString(); 
     labelC8.Text = complete[7].ToString(); 

     Application.DoEvents(); 

    } 
} 
+0

작동하지 않습니다. 처리를 스레딩하겠습니다. –

관련 문제