Click here to Skip to main content
15,886,074 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I've written an app that searches for files in hard disk , but this part of code without any error message or ... doesn't work.
code:

C#
private void Go_Click(object sender, EventArgs e)
        {
            drives = System.Environment.GetLogicalDrives();
            Thread t = new Thread(() => Engine(drives));
            t.Start();
        }
        private void Engine(string[] path)
        {
            foreach (string s in path)
            {
                try
                {
                    foreach (string f in System.IO.Directory.GetFiles(s))
                        list.Items.Add(f);
                    Engine(System.IO.Directory.GetDirectories(s));
                }
                catch { continue; }
            }
        }

can anyone tell me why it doesn't work?!
Thanks.
Posted
Updated 26-Jul-11 2:38am
v2

You cannot change UI controls (as ListView) from the thread. You have to do a Control.Invoke[^].

See How to: Manipulate Controls from Threads[^]
 
Share this answer
 
Comments
[no name] 26-Jul-11 8:51am    
Correct, and the fact that he consumes his exception in the catch block prevents him from seeing the exception.
mehdi_k 26-Jul-11 9:22am    
thanks for your answer.
this code works fine : this.Invoke((MethodInvoker)delegate { list.Items.Add(f); });
but is there any basic tutorial for "invoke"?
Kim Togo 26-Jul-11 9:37am    
You are welcome.

Yes, I find this CP article very useful.
C# Event Implementation Fundamentals, Best Practices and Conventions.
Sergey Alexandrovich Kryukov 27-Jul-11 1:32am    
This is the major problem here, my 5.
--SA
You are consuming your exception, that's the worst way to write a catch block. Put some code in your catch block to display/log your error message and you'll know why your code fails.
 
Share this answer
 
Comments
Kim Togo 26-Jul-11 8:57am    
Good advice.
Sergey Alexandrovich Kryukov 27-Jul-11 1:30am    
Agree, my 5.
--SA
I'm guessing list is a control? If so, that's your problem. You are making a cross thread access to a Windows control. That is a no-no. You need to do that list.Items.Add part back on your GUI thread.
How you do that depends on if you are using WPF or Windows forms. For WPF you use Dispatcher.Invoke or Dispatcher.BeginInvoke. For forms you use Control.Invoke or Control.BeginInvoke.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Jul-11 1:33am    
Correct, a 5.
--SA
The honest answer is no, not when you have a catch block which does not log the exceptions!

Alan.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Jul-11 1:33am    
Honestly, a 5.
--SA
In addition to what's already been posted, you shouldn't mix the business logic (what's running in the background thread) and the UI so closely. Instead, have the file browser emit events when it finds a new item, and have the UI code handle that event and do the Invoke.

(Actually, isn't it better to use BeginInvoke for something like this? Invoke is synchronous and therefore will result in pipelining all the threads into the UI update queue. For something this simple it doesn't matter but a lot of questions on here are about using Invoke or Begin/EndInvoke.)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Jul-11 1:34am    
Good point, a 5.
--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