2013-02-05 43 views
-2

我的進度條有問題,因爲直到數據加載時才顯示它。調用數據庫時ProgressBar不更新

我不確定我的代碼是對還是錯。請幫助解決這個問題,並打破我的頭腦。任何建議將是對我plzzz真正有用的..

progressBar1.Minimum = 0; 
progressBar1.Maximum = short.MaxValue; 
progressBar1.Value = 0; 

double value = 0; 

UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(progressBar1.SetValue); 

foreach (CDType ctp in dgAttributes.ItemsSource) 
{ 
    if (ctp.IsSelected == true) 
    { 
     //Mouse.OverrideCursor = Cursors.Wait; 
     var CDTypeID = ctp.TYPE_ID; 

     label3.Content = "Loading.." + CDTypeID; 

     SqlCommand sqlCommand = new SqlCommand(conn.ConnectionString); 
     sqlCommand.CommandType = CommandType.StoredProcedure; 
     sqlCommand.CommandText = "LOAD_DATA_SOURCE_SAVE"; 

     sqlCommand.Parameters.AddWithValue("CDTYPE_ID", CDTypeID); 

     sqlConnection.Open(); 
     sqlCommand.Connection = sqlConnection; 

     do 
     { 
      value += 1; 
      Dispatcher.Invoke(updatePbDelegate, 
           System.Windows.Threading.DispatcherPriority.Background, 
           new object[] { ProgressBar.ValueProperty, value });           
      //i'm not sure whether its correct way or not to give below line here..? I mean execute non query...since it was looping all the time untill its reaches Maximum value 
      //sqlCommand.ExecuteNonQuery(); 
     } 
     while (progressBar1.Value != progressBar1.Maximum); 

     sqlConnection.Close(); 
     //label3.Content = "Sucessfully Loaded..!"; 
    } 
} 
+1

請修復縮進。這並不難。 –

+0

@ user1990395你是否在UI線程中加載數據? –

+1

呃...乍看之下......你真的想讓你的進度條從0運行到32767嗎?這也可能是您使用「未知」進度時間的一個很好的例子,因爲您剛剛訪問數據庫時沒有明確的開始/結束值或「進度」概念。請參閱爲您的進度條使用「字幕」樣式:http://social.msdn.microsoft.com/forums/en-US/winforms/thread/9e51ad57-988e-4c17-98f0-cd2a8abec503/編輯:Ooops,WPF,只是使用'IsIndeterminate':http://msdn.microsoft.com/en-us/library/system.windows.controls.progressbar.isindeterminate%28VS.100%29.aspx –

回答

1

只要你不能確定它需要多長時間,你可能想使用一個進度條,其樣式設置爲不確定的。

<ProgressBar IsIndeterminate="True" /> 

如果使用UI線程加載數據,由於線程繁忙,Progressbar可能不會更新。請參閱this post關於如何解決使用後臺工作程序加載數據的問題。您也可以在StackOverflow上搜索;背景工作者有許多問題。

如果數據庫批量傳回數據,您可能可以跟蹤批量數量,並使用具有實際值的進度條。

+0

我'已經嘗試過BackgroundWorker的過程,但沒有爲我工作.... – user1990395

+0

然後你沒有做到這一點,因爲它確實有效。 –