0

I have some code below that goes through all the directories of a given area sDir it also runs on it's own thread. This builds a liststring that then gets converted to an array to build a treeview.

However I would also like to count the number of directories and files that the search is up to and display it on my UI. If i add the this.Invoke((MethodInvoker)(() => DirCL.Text = "Dir: " + dirC.ToString()));

and the this.Invoke((MethodInvoker)(() => FileCL.Text = "Files: " + FileC.ToString())); liststring.Add(Files);

This makes the function go from 10 seconds to do an area with 65,000 files to 100ish seconds.

I assume this Increase is because the MethodInvoker is pausing the function to go write to the UI.

Is there a better way for me to be writing code to the UI that is in a different thread?

int dirC = 0;
int FileC = 0;

private void DirSearch(string sDir)
        {
            try
            {
                string[] array1 = Directory.GetDirectories(sDir);
                for (int i1 = 0; i1 < array1.Length; i1++)
                {
                    string Dir = array1[i1];
                    dirC++;
                    //this.Invoke((MethodInvoker)(() => DirCL.Text = "Dir: " + dirC.ToString()));
                    try
                    {
                        String[] array = Directory.GetFiles(Dir, txtFile.Text);
                        for (int i = 0; i < array.Length; i++)
                        {
                            string Files = array[i];
                            FileC++;
                            //this.Invoke((MethodInvoker)(() => FileCL.Text = "Files: " + FileC.ToString()));
                            liststring.Add(Files);

                        }
                    }
                    catch (System.Exception excpt)
                    {
                        Console.WriteLine(excpt.Message);
                    }
                    DirSearch(Dir);
                }
            }
            catch (System.Exception excpt)
            {
                Console.WriteLine(excpt.Message);
                ErrC++;
            }

        }
adam Wadsworth
  • 774
  • 1
  • 8
  • 26
  • 2
    Use `BeginInvoke` instead of `Invoke` in any situation where you don't care about the result of the invocation immediately after the invocation. – Rotem Nov 29 '18 at 12:51
  • 9
    You are doing 65,000 files every 10 seconds. So, in 10 seconds you want this text to be changed (visually) 65,000 times. People can't read that fast. Change your code to change the text every 300 files (or 200, or 1000, or whatever) rather than on every file. – mjwills Nov 29 '18 at 12:51
  • Related, borderline duplicate: https://stackoverflow.com/questions/229554/whats-the-difference-between-invoke-and-begininvoke – Rotem Nov 29 '18 at 12:53
  • @adamWadsworth What happened when you tried my suggestion? – mjwills Nov 29 '18 at 20:20
  • @mjwills you were totally right, reducing the number of times that i displayed the counts fixed the issue it's now just finding a reasonable number to refresh. – adam Wadsworth Dec 01 '18 at 01:18

0 Answers0