简体   繁体   English

多线程和列表框

[英]Multi-Threading and Listbox

What I'm trying to do is get and remove the first item in a listbox with multiple threads, though I need to keep the threads from using the same item. 我想做的是获取和删除具有多个线程的列表框中的第一项,尽管我需要防止线程使用同一项。

Ex: Thread 1 and 2 both grab 1, and attempt to remove 1, but in the process, both threads use the same variable, and both 1 and 2 are removed since I'm using removeat(0). 例如:线程1和2都抢占1,并尝试删除1,但是在此过程中,两个线程都使用相同的变量,并且由于我正在使用removeat(0),所以删除了1和2。

Listbox for Ex 防爆列表框

1 1个

2 2

How would I go about managing the threads so that they don't use / remove the wrong variables? 我将如何管理线程,以免它们使用/删除错误的变量?

You can search the particular item in the list and remove it............. 您可以在列表中搜索特定项目并将其删除.......。

 foreach (string item in toRemove)
 {
   listbox.Remove(item);
 }

OR......... 要么.........

If you are assigning a datasource to it then add one more UNIQUE column like Primary Key and on the basis of that unique column you can remove item from LISTBOX 如果您要为其分配数据源,则再添加一个UNIQUE列,例如Primary Key,并在该唯一列的基础上,您可以从LISTBOX中删除项目

You could lock the threads on a reference (maybe the list ?) for the whole process of 您可以将线程锁定在引用(可能是列表)上,以用于

  1. Finding the item 寻找物品
  2. removing the item if it was found 如果找到该项目,则将其删除

HTH 高温超导

Use Lock when you access your listbox items. 访问列表框项目时使用锁定。 Use the same object for locking everywhere. 使用同一对象锁定所有位置。

http://msdn.microsoft.com/en-us/library/c5kehkcz%28v=vs.80%29.aspx http://msdn.microsoft.com/zh-CN/library/c5kehkcz%28v=vs.80%29.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM