Search code examples
c#formslag

C# Form is unresponsive when scanning for files


I made a program that scans trough a user selected folder, it will search for MP3 files and WAV files then it will calculate the total size of these files but when its doing all this the program is unresponsive labels will not update or just half and the window can't be dragged.

How do I fix this problem??

Link to download my program : LINK Link to virus total : LINK

EDIT :

Here is the code that checks the file size's :

    public string testFileSize(String dir)
    {
        if (MFILE_OPTION_SUBFOLDER_CHECKBOX.Checked == true)
        {
            double totalFileSize = 0;

            if (MFILE_OPTION_MP3_CHECKBOX.Checked == true)
            {
                string[] files = System.IO.Directory.GetFiles(dir, "*.mp3", SearchOption.AllDirectories);
                foreach (string fileName in files)
                {
                    FileInfo fi = new FileInfo(fileName);

                    double fileSize = fi.Length;
                    totalFileSize += fileSize;
                }
            }

            if (MFILE_OPTION_WAV_CHECKBOX.Checked == true)
            {
                string[] files = System.IO.Directory.GetFiles(dir, "*.wav", SearchOption.AllDirectories);
                foreach (string fileName in files)
                {
                    FileInfo fi = new FileInfo(fileName);

                    double fileSize = fi.Length;
                    totalFileSize += fileSize;
                }
            }

            totalFileSize = Math.Round((totalFileSize / 1024f) / 1024f, 2);
            return totalFileSize.ToString() + " MB";
        }
        else
        {
            double totalFileSize = 0;

            if (MFILE_OPTION_MP3_CHECKBOX.Checked == true)
            {
                string[] files = System.IO.Directory.GetFiles(dir, "*.mp3", SearchOption.TopDirectoryOnly);
                foreach (string fileName in files)
                {
                    FileInfo fi = new FileInfo(fileName);

                    double fileSize = fi.Length;
                    totalFileSize += fileSize;
                }
            }

            if (MFILE_OPTION_WAV_CHECKBOX.Checked == true)
            {
                string[] files = System.IO.Directory.GetFiles(dir, "*.wav", SearchOption.TopDirectoryOnly);
                foreach (string fileName in files)
                {
                    FileInfo fi = new FileInfo(fileName);

                    double fileSize = fi.Length;
                    totalFileSize += fileSize;
                }
            }

            totalFileSize = Math.Round((totalFileSize / 1024f) / 1024f, 2);
            return totalFileSize.ToString() + " MB";
        }
    }

This is what I tried but them with all the file types as shown above ^^

                if (MFILE_OPTION_WAV_CHECKBOX.Checked == true)
            {
                BackgroundWorker bw = new BackgroundWorker();
                bw.DoWork += (s, e) =>
                {
                    string[] files = System.IO.Directory.GetFiles(dir, "*.wav", SearchOption.TopDirectoryOnly);
                    foreach (string fileName in files)
                    {
                        FileInfo fi = new FileInfo(fileName);

                        double fileSize = fi.Length;
                        totalFileSize += fileSize;
                    }
                };

                bw.RunWorkerAsync();
            }

But this returns 0 MB how?


Solution

  • You should use a BackgroundWorkerfor this. If you would supply some code we could help you better. So far I can only give you this:

    BackgroundWorker bw = new BackgroundWorker();
    bw.DoWork += (s,e) =>
       {
          // Do your work
       };
    
    bw.RunWorkerAsync();
    

    Example based on your code:

    if (MFILE_OPTION_WAV_CHECKBOX.Checked == true)
            {
                BackgroundWorker bw = new BackgroundWorker();
                bw.DoWork += (s, e) =>
                {
                    string[] files = System.IO.Directory.GetFiles(dir, "*.wav", SearchOption.TopDirectoryOnly);
                    foreach (string fileName in files)
                    {
                        FileInfo fi = new FileInfo(fileName);
    
                        double fileSize = fi.Length;
                        totalFileSize += fileSize;
                    }
                };
    
                bw.RunWorkerCompleted += (s,e) =>
                {
                        //Update GUI
                 }
    
                bw.RunWorkerAsync();
            }