我将项目添加到列表视图中,但我需要能够为每个项目运行一个线程以获取该项目的更多数据(需要更长时间才能获得) 这只是我现在尝试的一个小样本,但由于某种原因,线程打印约5项左右的名称然后等待5秒
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
Console.WriteLine((string)e.Argument);
Thread.Sleep(5000);
}
BackgroundWorker[] threadArray = new BackgroundWorker[items.Count];
int gIndex = -1;
foreach (Item gItem in items.Values)
{
gIndex++;
ItemDescription ID = getDescription(gItem.id);
_ListViewAddRow(gIndex, ID.name, ID.url);
threadArray[gIndex] = new BackgroundWorker();
threadArray[gIndex].DoWork += new DoWorkEventHandler(worker_DoWork);
threadArray[gIndex].RunWorkerAsync(ID.name);
}
答案 0 :(得分:0)
正如@steve提到的那样 使用一个后台工作者。
BackgroundWorker threadArray = new BackgroundWorker;
List<string> list = new List<string>();
foreach (Item gItem in items.Values)
{//your code
list.Add(ID.name);
}
threadArray.RunWorkerAsync(list);
现在在dowork中将值转换为字符串列表 做工作方法
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
List<string> genericlist = e.Argument as List<string>;//cast
for (int i = 0; i < genericlist.Count; i++) {
Console.WriteLine(genericlist[i]);
Thread.Sleep(5000);
}
}