C# 从远程处理服务器逐个使用sqldata填充列表框
实际上,我正在用sqldata填充一个C# 从远程处理服务器逐个使用sqldata填充列表框,c#,wpf,listbox,C#,Wpf,Listbox,实际上,我正在用sqldata填充一个列表框,它正在工作,只是请求结束后数据才会出现。我一直试图在这里使用线程,但我对如何实现这一点迷失了方向我也尝试过为做一个循环,它会逐个加载我的列表框中的数据,但是加载所有数据需要很长时间,我不希望这样 下面是我如何填充我的列表框: internal delegate void SetDataSourceDelegate(List<DataDerogationObjects.DerogationUPRArgs> list); private vo
列表框
,它正在工作,只是请求结束后数据才会出现。我一直试图在这里使用线程,但我对如何实现这一点迷失了方向我也尝试过为做一个
循环,它会逐个加载我的列表框
中的数据,但是加载所有数据需要很长时间,我不希望这样
下面是我如何填充我的列表框
:
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);
}
}
克减记录
是一个包含诸如id
、name
等信息的类。
请随时询问更多信息。谢谢
[编辑]
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;
}
公共列表列表克减()
{
List returnList=新列表();
OleDbCommand=connection.CreateCommand();
command.CommandText=“从QuotaCarte中选择不同的QuotaCarte.IDCarteQuota、Nom、CodePostal、maxQtType1、maxQtType2、maxQtType3、maxQtType4、maxQtType5、DateValidité、idCarteJointe1、QtType1、QtType2、QtType3、QtType4、QtType5、日期创建、分类客户端、UCASE(Nom)”+
“左连接QuotaCarteExtend on QuotaCarte.IDCarteQuota=QuotaCarteExtend.IDCarteQuota,其中子字符串(QuotaCarte.IDCarteQuota,1,6)='900000'按UCASE排序(Nom)”;
int total=CountDBRecord(),count=0;
OleDbDataReader=command.ExecuteReader(System.Data.CommandBehavior.SingleResult);
while(reader.Read())
{
object[]tableau=新对象[20];
reader.GetValues(tableau);
DataDecreationObjects.DecreationUpArgs readed=新的DataDecreationObjects.DecreationUpArgs();
readed.decreation=ConvertReadedObjectTableToDerogationRecord(tableau);
计数++;
readed.totalNumber=总数;
readed.actualNumber=计数;
返回列表。添加(已读取);
}
退货清单;
}
您应该使用Task来实现您想要的目标
TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
这允许您从一个线程(而不是UI线程)更新视图
然后必须将其传递给任务构造函数:
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);
如果有任何疑问,请直接“开枪”:)我应该在哪里使用它?在我的setDataList
中,在我的if
中,这是我在您的代码中不理解的部分。iDerog.listDecreation();获取所有信息,对吗?那你想要什么?要循环并逐个元素添加元素,对吗?iDerog.listdecretion()实际上从我的数据库中获取所有数据。这就是我被卡住的地方,我想在加载数据时更新我的列表框。但是我不能在我的iDerog.ListDecreation()中执行此操作。唯一的方法是更改ListDecreation()方法。这是你唯一知道信息的地方。您能在该方法中显示代码吗?使用ListDecreation()方法编辑。
TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
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);