我的進度條有問題,因爲直到數據加載時才顯示它。調用數據庫時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..!";
}
}
請修復縮進。這並不難。 –
@ user1990395你是否在UI線程中加載數據? –
呃...乍看之下......你真的想讓你的進度條從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 –