Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to display a big directory (4000 files) in a ListView with icons fast in one second like Windows Explorer does without freezing my Form1 UI.

For it I am trying to make use of the BackGroundWorker to make my Form1 not being freezed while the files are being loaded.
I ran into following problem:

I am getting exceptions like that: InvalidOperationException Thread-Crosswise...
Cannot access ListView in another thread...
Cannot access ImageList in another thread...

This is what I tried: I asked a professional and he said it is an error / limitation in the free VS 2008 Express edition. I have to buy the Ultimate Edition to remove the limitation. I asked Microsoft Support on this too and they said also that I have to own the full version to fix that bug. Now I got the full version (Ultimate Version), but the bug still prevails.
I uninstalled already old version and re-installed Ultimate and re-created the same project. What is still wrong with it?

Another expert on expert-exchange said I have to get the Ultimate Edition and then invoke it. I invoked the listView1 and this worked with this bugfix code:

C#
if (listView1_.InvokeRequired)
{
    listView1_.Invoke(new MethodInvoker(delegate
    {
        listView1_.Items.Add("file");
    }));
}
else
{
    listView1_.Items.Add("file");
}



But the ImageList has no such invoke option. I found nothing understandable and satisfying for me to solve the ImageList problem in the same way unless I drop the icons which makes for me no sense. Any ideas?
Posted

1 solution

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke(),
Problem with Treeview Scanner And MD5.

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net,
Control events not firing after enable disable + multithreading.

—SA
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 13-Mar-15 11:24am    
Not sure. There are all my posts. Thank you very much for this note, I'll check up later.
But even the rest of the answer should give you the idea on what to do.
—SA
Sergey Alexandrovich Kryukov 22-Mar-15 11:56am    
I cannot. Probably all four questions were later removed. Restoring it would take time.
Would you simply read MSDN pages on all the methods I mentioned in the first paragraphs? If you have problems using them, please send me a comment and ask further questions, I'll try to answer.

I restored the links, thank you!

Will you accept the answer formally now?

—SA
Sergey Alexandrovich Kryukov 28-Mar-15 16:33pm    
FYI.
I reported the link problem. Recently, the problem was resolved for the whole CodeProject site.
Thank you for reporting to problem to me; it was the initial reason to address the problem.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900