firefox wrote:
Hola Foro!, tengo una rutina que permite agregar varios elementos o
items de una sola vez en un ListView. Lo que sucede es que en el
momento que se añaden los items al ListView se pone todo blanco,
cuando se terminan de agregar los items vuelve a su estado normal. Como
impido que esto suceda ?. Gracias Mil.
Translated again:
Hello Forum! I have a method that adds various rows or items all at
once to a ListView. What happens is that when adding the items to the
ListView it goes completely blank. When the items are all added to the
list view everything returns to normal. How can I prevent this from
happening? Thanks very much.
My response:
I have the same problem. I know why it happens, but because of the
structure of my application I haven't figured out exactly how I'm going
to solve it.
The first thing to understand is that unless you do something special
with Threads, all of the processing you do is in the user interface
(UI) thread, and while you are doing work, the Form cannot redraw
itself, so it appears frozen on the screen. It may go blank, or it may
retain the image of something that was moved over top of it, or it may
stay intact but simply not respond.
In my case, I'm building arrays of columnar data, then creating
thousands of ListViewItem objects, one from each array, and then adding
the ListViewItems to the ListView, all in one operation. This causes
the UI thread to block for an uncomfortable length of time, and the
application "freezes" on the screen.
The solution, as I understand it, is to do as much work as possible on
a background thread. Unfortunately, I don't know whether it's valid to
create a ListViewItem on a background thread, because background
threads cannot make any calls to user interface controls. This may (I
don't know) include not being able to create a new ListViewItem.
Even if I could build everything on a background thread and then only
afterward add it all to the ListView, I still have the problem that
callers to my ListViewForSelfKeyedCollection object (which encapsulates
a ListView) expect to assign a collection of items to the list and then
be able to immediately set SelectedItem, etc. As I said, I need to work
on the program structure.
Does anyone else have a solution to this sort of problem? Is there a
white paper or tutorial out there on dealing with this? I'd appreciate
being pointed in the right direction, as would firefox, I'm sure.
My response translated (badly):
Tengo el mismo problema. Yo se por que sucede, per a cause de la
estructura de mi programa, todavia no tengo solucion.
Hay que intender que todo las operaciones que haces estan hechas por el
"thread" del UI, a menos que estas usando "Threads". Y si estas
haciendo todo el trabajo en el "thread" del UI, el Form no se puede
dibujar mientras. Entonces, parace "conjelado", o posibilmente se
borra, o simplemente no responde.
En mi caso, estoy construiendo "arrays" de data columnar, y despues
creando miles de ListViewItem, cada uno de uno de los "arrays", y
despues anadandoles al ListView, todo de una vez. Asi el "thread" del
UI esta bloqueado para un tiempo largo, que se nota, y mi programa
"congela" para un largo tiempo.
La solucion, como lo entiendo, es hacer el maximo trabajo en un
"background thread". Tristemente, no se si vale crear un ListViewItem
en el "background", por que un "background thread" esta prohibido
accessar a elementos del UI (Controls).
Aunque podria crear todo en un "background thread" y solo despues
anadirlo todo al ListView, tengo siempre el problema que los que usan
mi ListViewForSelfKeyedCollection (que encierra un ListView) van a
esperar que puedan anadir los elementos del ListView y inmediatemente
selectionar uno, etc. Necesito trabajor en la estructura de mi programa.