简体   繁体   中英

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).

Listbox for Ex

1

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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