2015-10-20 93 views
3

沒有做循環,我的代碼運行良好。只要我將它放在do或while循環中,代碼就無法更新顏色狀態。任何想法?從我從互聯網上收集的信息,我的循環被正確寫入。爲什麼我的循環不允許我的標籤顏色改變?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Net.NetworkInformation; 
using System.Threading; 



namespace SystemsUpDown 
{ 
    public partial class MainForm : Form 
    { 

     public MainForm() 
     { 
      InitializeComponent(); 
     } 

     bool ContinuePing = false; 


     private void QuitButton_Click(object sender, EventArgs e) 
     { 
      this.Close(); 
     } 

     private void StartButton_Click_1(object sender, EventArgs e) 
     { 
      Ping ping = new Ping(); 
      ContinuePing = true; 
      do 
      { 
       try ///ping google 
       { 
        PingReply reply = ping.Send("8.8.8.8"); 

        if (reply.Status == IPStatus.Success) 
        { 
         GoogleStatusLabel.BackColor = Color.Green; 
        } 

       } 
       catch 
       { 
        GoogleStatusLabel.BackColor = Color.Red; 
       } 

       try ///ping Yahoo! 
       { 
        PingReply reply = ping.Send("www.yahoo.com"); 

        if (reply.Status == IPStatus.Success) 
        { 
         YahooStatusLabel.BackColor = Color.Green; 
        } 

       } 
       catch 
       { 
        YahooStatusLabel.BackColor = Color.Red; 
       } 

       try ///ping Reddit.com 
       { 
        PingReply reply = ping.Send("www.reddit.com"); 

        if (reply.Status == IPStatus.Success) 
        { 
         RedditStatusLabel.BackColor = Color.Green; 
        } 

       } 
       catch 
       { 
        RedditStatusLabel.BackColor = Color.Red; 
       } 

       try ///ping Chive 
       { 
        PingReply reply = ping.Send("www.chive.com"); 

        if (reply.Status == IPStatus.Success) 
        { 
         ChiveStatusLabel.BackColor = Color.Green; 
        } 

       } 
       catch 
       { 
        ChiveStatusLabel.BackColor = Color.Red; 
       } 

       try ///ping CNN 
       { 
        PingReply reply = ping.Send("www.cnn.com"); 

        if (reply.Status == IPStatus.Success) 
        { 
         CNNStatusLabel.BackColor = Color.Green; 
        } 

       } 
       catch 
       { 
        CNNStatusLabel.BackColor = Color.Red; 
       } 

      } while (ContinuePing); 

     } 

     private void StopButton_Click(object sender, EventArgs e) 
     { 
      GoogleStatusLabel.BackColor = Color.Yellow; 
      ChiveStatusLabel.BackColor = Color.Yellow; 
      CNNStatusLabel.BackColor = Color.Yellow; 
      RedditStatusLabel.BackColor = Color.Yellow; 
      YahooStatusLabel.BackColor = Color.Yellow; 
      ContinuePing = false; 

     } 
    } 
} 
+1

因爲你沒有給UI隨時更新,而不是使用do..loop,使用'System.Windows .Forms.Timer'並定期運行它。 –

+1

同意。該線程將永遠忙於'StartButton_Click_1',所以即使其他事件已經觸發並且「StopButton_Click」處於「正在隊列中」,它將永遠不會運行,因爲其他方法永遠不會結束。當你修正它時,考慮將字段'ContinuePing'放到'volatile'字段中。 –

回答

1

嘗試強制標籤的刷新你改變它的顏色後:

GoogleStatusLabel.Refresh(); 
+0

這樣做。它讓我想到另一個問題,因爲像Pieter21這樣的線程提到過。但它爲我所問的問題工作。謝謝! –

+0

正確 - 因爲你的循環剛剛繼續,UI永遠不會有機會刷新。如果您使用單獨的線程,則不會鎖定用戶界面。 –

0

你的過程是在不同的線程比UI線程,這沒有得到及時更新UI連續運行項目。

使用更新/刷新迫使UI一些更新時間,或使用睡眠讓UI線程有一段時間

+0

我不認爲這是真的,任何線程都不會在任何地方啓動 - 它全部發生在從UI線程開始的'button_click'內 - 很可能只是UI不更新,因爲它一直在運行,並且沒時間更新。 – Ric

+0

我所擁有的是更新的顏色,但是我無法與UI進行交互,因爲它現在陷入了一個不變的循環。這是我現在正在處理的另一個問題。 –

+0

對不起,你是對的,當我有類似的問題時,我太在意發生的事情。誰沒有;)。這仍然是沒有時間更新的問題,但不是在不同的線程上 – Pieter21

1

這可能是因爲在循環運行速度不夠快,因此更新不理解。

嘗試增加循環中的某睡眠就像

//Sleep for two seconds. You can add this at end of loop. 
//Or, sleep for 2 secs after pinging one site. 
// 2000 miliseconds = 2 seconds. 
System.Threading.Thread.Sleep(2000); 

再檢查。

我想建議您爲此使用BackgroundWorker。由於主線程中的while循環將掛起主窗口窗體。

BackgroundWorker適用於窗口窗體控件的定期更新,以便主窗口不會掛起。 請參考以下鏈接 - https://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx

+1

在UI線程中睡覺不會允許UI更新,事實上,如果延遲足夠長(5秒),您可能會得到「不響應」,並且它不需要是單個延遲,循環會導致它。 'BackgroundWorker'存在需要注意的跨線程問題。 –

+1

問題是UI消息泵沒有運行,所以根本沒有在UI上進行實際的更新。導致顯示更新的消息正在排隊等待循環退出,此時消息得到處理後最後的更改將顯示。 – Corey

0

馬克的Click()處理與async,然後把你的循環到await Task.Run()與匿名委託。使用更新的this.Invoke() UI,我已經有一個輔助方法如下進行:

private async void StartButton_Click_1(object sender, EventArgs e) 
    { 
     await Task.Run(() => 
     { 
      Ping ping = new Ping(); 
      ContinuePing = true; 
      do 
      { 
       try ///ping google 
       { 
        PingReply reply = ping.Send("8.8.8.8"); 

        if (reply.Status == IPStatus.Success) 
        { 
         SetLabelColor(GoogleStatusLabel.BackColor, Color.Green); 
        } 

       } 
       catch 
       { 
        SetLabelColor(GoogleStatusLabel.BackColor, Color.Red); 
       } 

       try ///ping Yahoo! 
       { 
        PingReply reply = ping.Send("www.yahoo.com"); 

        if (reply.Status == IPStatus.Success) 
        { 
         SetLabelColor(YahooStatusLabel.BackColor, Color.Green); 
        } 

       } 
       catch 
       { 
        SetLabelColor(YahooStatusLabel.BackColor, Color.Red); 
       } 

       try ///ping Reddit.com 
       { 
        PingReply reply = ping.Send("www.reddit.com"); 

        if (reply.Status == IPStatus.Success) 
        { 
         SetLabelColor(RedditStatusLabel.BackColor, Color.Green); 
        } 

       } 
       catch 
       { 
        SetLabelColor(RedditStatusLabel.BackColor, Color.Red); 
       } 

       try ///ping Chive 
       { 
        PingReply reply = ping.Send("www.chive.com"); 

        if (reply.Status == IPStatus.Success) 
        { 
         SetLabelColor(ChiveStatusLabel.BackColor, Color.Green); 
        } 

       } 
       catch 
       { 
        SetLabelColor(ChiveStatusLabel, Color.Red); 
       } 

       try ///ping CNN 
       { 
        PingReply reply = ping.Send("www.cnn.com"); 

        if (reply.Status == IPStatus.Success) 
        { 
         SetLabelColor(CNNStatusLabel, Color.Green); 
        } 

       } 
       catch 
       { 
        SetLabelColor(CNNStatusLabel, Color.Red); 
       } 

      } while (ContinuePing); 
     }); 
    } 

    private void SetLabelColor(Label lbl, Color clr) 
    { 
     this.Invoke((MethodInvoker)delegate { 
      lbl.BackColor = clr; 
     }); 
    } 
相關問題