2012-03-16 55 views
1

顯示一個旋轉輪進度動畫GIF,而用戶啓動一個長時間運行的過程。 當我點擊開始,過程開始,同一時間輪開始旋轉。C#,winform - Spining輪子進度中斷並間歇性地恢復

但問題是,輪子在中間和繼續,在長期過程中發生多次。它應該是連續旋轉的。我在同一個線程中運行任務和動畫gif(,因爲指標只是一個動畫圖像,不是真實的進度值)。使用

守則,

 this.progressPictureBox.Visible = true; 
     this.Refresh(); // this - an user controll 
     this.progressPictureBox.Refresh(); 
     Application.DoEvents(); 
     OnStartCalibration(); // Starts long running process 
     this.progressPictureBox.Visible = false; 

OnStartCalibration() 
    {  

     int count = 6; 
     int sleepInterval = 5000; 
     bool success = false; 
     for (int i = 0; i < count; i++) 
     { 
      Application.DoEvents(); 
      m_keywordList.Clear(); 
      m_keywordList.Add("HeatCoolModeStatus"); 
      m_role.ReadValueForKeys(m_keywordList, null, null); 
      l_currentValue = (int)m_role.GetValue("HeatCoolModeStatus"); 
      if (l_currentValue == 16) 
      { 
       success = true; 
       break; 
      }  
      System.Threading.Thread.Sleep(sleepInterval); 
     } 
} 

如何顯示輪的未interepted連續顯示,直到進程結束?

+0

請貼在你是如何處理長期運行過程.. BackgroundWorker的一些代碼? BeginInvoke的? – 2012-03-16 06:46:47

回答

0

您無法在同一個線程上運行進度指示和任務。您應該使用一個BackgroundWorker

您的GUI線程將訂閱ProgressChanged事件,並且會通知任務的更新。從這裏,您可以適當更新進度指示。還有任務完成時的事件。

1

如果您使用的框架4,請用下面的代碼OnStartCalibration(); // Starts long running process行:

BackgroundWorker bgwLoading = new BackgroundWorker(); 
bgwLoading.DoWork += (sndr, evnt) => 
{ 
    int count = 6; 
    int sleepInterval = 5000; 
    bool success = false; 
    for (int i = 0; i < count; i++) 
    { 
     Application.DoEvents(); 
     m_keywordList.Clear(); 
     m_keywordList.Add("HeatCoolModeStatus"); 
     m_role.ReadValueForKeys(m_keywordList, null, null); 
     l_currentValue = (int)m_role.GetValue("HeatCoolModeStatus"); 
     if (l_currentValue == 16) 
     { 
      success = true; 
      break; 
     }  
     System.Threading.Thread.Sleep(sleepInterval); 
    } 
}; 
bgwLoading.RunWorkerAsync();