A
Andrius B.
Hi all.
I am developing a project, one part of which is reading data from MDB
(Access DB file), performing some calculation with the data and then
displaying in ListView.
The problem is the time needed to fill the Listview control. There are more
than 5000 items (with 15 subitems on each item) to display, despite the
using of Listview.Items.Addrange method.
As one way for the solution could be using multithreading and a Hashtable
object; this Hashtable is a declared as friend variable (Friend Queue as
Hashtable)
So, one function runs in one current thread, doing reading from MDB and
calculation (calculation takes not so less time), and adding the data to
ListviewItem object, and this ListviewItem is added to
hashtable: Queue.Add(key, ListviewItemobj). Thus, the quantity of the
objects stored in Queue is increasing
Another function runs in another thread and could perform reading at the
same time the content of Queue, getting the ListviewItem object from it one
by one, and then adding them to the Listview using Listview.Items.Add (item
as listviewitem) method.
The problem is for this second function - to find out which item is already
taken from Queue and inserted into Listview, and which are not, in order to
avoind duplicate inserting.
For this purpose, the code should remove the key/value (key/listviewitem in
my case) pair, which are no longer needed, from the Queue. But the objects
in the Hashtable could be accessed only by Keys. So, I must use smth like
Keys.IEnumerable interface for to hashtable object, calling .Movenext and
..Current of the IEnumerable object. But that produces an exception
("Collection has been changed, Enumaration will not go on" or smth like
that), because both threads use the same Hashtable object Queue
simultaneosly. Should I used some kind of locking the Queue object in order
the first thread could perform no writing to it? But in such a case, I would
loose some time. Doing for...each cycle many times for the Queue, while the
quantity of items in which is only increasing, cannot also be a good
solution.
By the way, the principle in the issue described is like performing some
task in the human life. E.g., we have a lot of books on the high shell, and
two workers for a task to bring all the book to the truck-car. The first
worker takes one or some books from the shell and puts them on the floor
formating a heap. Simultaneosly, the second worker comes and takes one or
some books from the top of the heap and brings them to the truck. Sometimes
the first worker works faster than the second, and the heap increases.
Sometimes the second does, and the heap becomes smaller or even diseappers,
because the second worker takes the last books from the floor, and the first
worker at this moment is just taking books from the shell.
So, the second worker should not wait till all the books will be in the
heap, and only then begin to carry them to the truck. It would be a nonsense
The same thing (simultaneosly working) I wuold like to do in my project.
Sorry for long "story". I just wanted to explain my problem as clear as
possible.
Thanks for any ideas.
I am developing a project, one part of which is reading data from MDB
(Access DB file), performing some calculation with the data and then
displaying in ListView.
The problem is the time needed to fill the Listview control. There are more
than 5000 items (with 15 subitems on each item) to display, despite the
using of Listview.Items.Addrange method.
As one way for the solution could be using multithreading and a Hashtable
object; this Hashtable is a declared as friend variable (Friend Queue as
Hashtable)
So, one function runs in one current thread, doing reading from MDB and
calculation (calculation takes not so less time), and adding the data to
ListviewItem object, and this ListviewItem is added to
hashtable: Queue.Add(key, ListviewItemobj). Thus, the quantity of the
objects stored in Queue is increasing
Another function runs in another thread and could perform reading at the
same time the content of Queue, getting the ListviewItem object from it one
by one, and then adding them to the Listview using Listview.Items.Add (item
as listviewitem) method.
The problem is for this second function - to find out which item is already
taken from Queue and inserted into Listview, and which are not, in order to
avoind duplicate inserting.
For this purpose, the code should remove the key/value (key/listviewitem in
my case) pair, which are no longer needed, from the Queue. But the objects
in the Hashtable could be accessed only by Keys. So, I must use smth like
Keys.IEnumerable interface for to hashtable object, calling .Movenext and
..Current of the IEnumerable object. But that produces an exception
("Collection has been changed, Enumaration will not go on" or smth like
that), because both threads use the same Hashtable object Queue
simultaneosly. Should I used some kind of locking the Queue object in order
the first thread could perform no writing to it? But in such a case, I would
loose some time. Doing for...each cycle many times for the Queue, while the
quantity of items in which is only increasing, cannot also be a good
solution.
By the way, the principle in the issue described is like performing some
task in the human life. E.g., we have a lot of books on the high shell, and
two workers for a task to bring all the book to the truck-car. The first
worker takes one or some books from the shell and puts them on the floor
formating a heap. Simultaneosly, the second worker comes and takes one or
some books from the top of the heap and brings them to the truck. Sometimes
the first worker works faster than the second, and the heap increases.
Sometimes the second does, and the heap becomes smaller or even diseappers,
because the second worker takes the last books from the floor, and the first
worker at this moment is just taking books from the shell.
So, the second worker should not wait till all the books will be in the
heap, and only then begin to carry them to the truck. It would be a nonsense
The same thing (simultaneosly working) I wuold like to do in my project.
Sorry for long "story". I just wanted to explain my problem as clear as
possible.
Thanks for any ideas.