从远程处理服务器逐个填充sqldata列表框

时间:2014-04-17 09:28:51

标签: c# wpf listbox

我实际上使用sqldata填充ListBox并且它正常工作,但只有在请求结束后才会显示数据。我一直试图在这里使用线程,但我对如何实现这一点感到迷茫。我还试图进行for循环,它会逐一加载ListBox中的数据,但是需要花费很长时间来加载所有数据,我不想要那个。

以下是我填充ListBox的方法:

internal delegate void SetDataSourceDelegate(List<DataDerogationObjects.DerogationUPRArgs> list);
private void setDataToList(List<DataDerogationObjects.DerogationUPRArgs> list)
{
   if (pgDerog.wrapItemControl.Dispatcher.Thread == Thread.CurrentThread)
   {
      pgDerog.wrapItemControl.ItemsSource = list.Select(x => x.derogation);
   }
   else
   {
      pgDerog.wrapItemControl.Dispatcher.BeginInvoke(new SetDataSourceDelegate(setDataToList), list);
   }

}

以下是我如何称呼它:

//Some code...

Thread thread = new Thread(new ThreadStart(loadData));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();

//Some code...

void loadData()
{
   List<DataDerogationObjects.DerogationUPRArgs> tmpList = new List<DataDerogationObjects.DerogationUPRArgs>();
   tmpList = iDerog.listDerogation(); // This is my sql which return a list of DataDerogationObjects.DerogationUPRArgs
   setDataToList(tmpList);
}

这就是我的DataDerogationObjects.DerogationUPRArgs看起来像

[Serializable]
public class DerogationUPRArgs : EventArgs
{
   public DerogationRecord derogation { get; set; }
   public int totalNumber { get; set; }
   public int actualNumber { get; set; }
}

DerogationRecord是一个包含idname等信息的类。
随意询问更多信息。谢谢!

[编辑]

public List<DataDerogationObjects.DerogationUPRArgs> listDerogation()
{
   List<DataDerogationObjects.DerogationUPRArgs> returnList = new List<DataDerogationObjects.DerogationUPRArgs>();
   OleDbCommand command = connection.CreateCommand();
   command.CommandText = "select distinct QuotaCarte.IDCarteQuota, Nom , CodePostal, maxQtType1, maxQtType2, maxQtType3, maxQtType4, maxQtType5, DateValidité, idCarteJointe1, QtType1, QtType2, QtType3, QtType4, QtType5, dateCreation, CategorieClient, UCASE(Nom) from quotaCarte " +
                "left join QuotaCarteExtend on QuotaCarte.IDCarteQuota = QuotaCarteExtend.IDCarteQuota where substring(QuotaCarte.IDcarteQuota, 1,6) = '900000' order by UCASE(Nom)";
   int total = CountDBRecord(), count = 0;

   OleDbDataReader reader = command.ExecuteReader(System.Data.CommandBehavior.SingleResult);
   while (reader.Read())
   {
      object[] tableau = new object[20];
      reader.GetValues(tableau);
      DataDerogationObjects.DerogationUPRArgs readed = new DataDerogationObjects.DerogationUPRArgs();
      readed.derogation = ConvertReadedObjectTableToDerogationRecord(tableau);
      count++;
      readed.totalNumber = total;
      readed.actualNumber = count;

      returnList.Add(readed);

   }
   return returnList;

}

1 个答案:

答案 0 :(得分:0)

您应该使用Task来实现您想要的目标。

TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();

这允许您从不是UI线程的线程更新View。

然后你必须将它传递给Task构造函数:

Task.Factory.StartNew( () => 
  {
 // here you should update your listbox with the item you want...do the process where you get and update the listbox.
  }, CancellationToken.None, TaskCreationOptions.None, uiScheduler);

任何疑惑只是&#34;射击&#34; :)