UI freezes when running program for searching files in whole system using C# Tasks
<p>I have written a small program which searches for some kind of files in whole syatem and after search is complete it shows the result in a list view but when I run this program my form freezes and i can not do anything.</p>

<p>Can someone please help in trying to fix the problem , below is the code I have written</p>

<pre><code>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
using System.IO.Compression;
using System.Security.AccessControl;


namespace SearchAndZipUtility
{
    public partial class MainForm : Form
    {
        private string _destFolder;
        private string _sourceToSearch = @"E:\New Books";
        private TaskScheduler schedular = TaskScheduler.Current;
        private CancellationTokenSource _ct = new CancellationTokenSource();
        private List&lt;string&gt; files = new List&lt;string&gt;();
        private string _selectedFileType;

        public MainForm()
        {
            InitializeComponent();
            toolStripStatusInfo.Visible = false;
            btnZip.Enabled = false;
        }

        protected override void OnLoad(EventArgs e)
        {
            // bind file types to combo box
            PopulateComboList();
            if (cmbFileTypes.Items.Count &gt; 0)
            {
                cmbFileTypes.SelectedIndex = 0;
            }
        }

        private void PopulateComboList()
        {
            cmbFileTypes.Items.AddRange(FileTypes.GetFileTypes());
        }

        private void btnDest_Click(object sender, EventArgs e)
        {
            DialogResult dr = folderBrowserDialog.ShowDialog();

            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                _destFolder = folderBrowserDialog.SelectedPath;
                txtDestFolder.Text = _destFolder;
            }
        }

        private void btnStartSearch_Click(object sender, EventArgs e)
        {
            toolStripStatusInfo.Visible = true;
            btnStartSearch.Enabled = false;
            fileListView.Items.Clear();
            fileListView.Refresh();
            _selectedFileType = cmbFileTypes.SelectedItem.ToString();

            List&lt;Task&gt; taskList = new List&lt;Task&gt;();
            DriveInfo[] drives = DriveInfo.GetDrives().Where(drive =&gt; drive.DriveType == DriveType.Fixed).ToArray();

            try
            {
                foreach (DriveInfo d in drives)
                {
                    DriveInfo dInfo = d;
                    //Task searchTask = Task.Factory.StartNew( () =&gt; { Fi}, _ct.Token, TaskCreationOptions.LongRunning, TaskScheduler.FromCurrentSynchronizationContext());
                    Task searchTask = Task.Factory.StartNew(() =&gt; { FindFiles(dInfo.RootDirectory, ref files); }
                                                                            , _ct.Token, TaskCreationOptions.LongRunning
                                                                            , TaskScheduler.FromCurrentSynchronizationContext());

                    System.Diagnostics.Trace.WriteLine("currently reading" + d.RootDirectory.FullName);
                    taskList.Add(searchTask);

                    searchTask.ContinueWith(PopulateResultList, TaskScheduler.FromCurrentSynchronizationContext());

                }

                Task.Factory.ContinueWhenAll(taskList.ToArray(), OnSearchCompleted, _ct.Token, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());

            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message);
            }
            finally
            {

            }


        }

        private void OnSearchCompleted(Task[] tasks)
        {
            // hide notifier label
            toolStripStatusInfo.Visible = false;
            btnStartSearch.Enabled = true;
            btnZip.Enabled = true;
            toolStripStatusLabel.Text = string.Format("Total files found: {0}", files.Count);
        }

        private List&lt;string&gt; SearchFiles()
        {
            // DirectoryInfo dirSource = new DirectoryInfo(_sourceToSearch);
            _selectedFileType = cmbFileTypes.SelectedItem.ToString();
            return files;
        }

        private void FindFiles(DirectoryInfo directoryInfo, ref List&lt;string&gt; files)
        {
            FileSystemAccessRule rule = new FileSystemAccessRule(System.Security.Principal.WindowsIdentity.GetCurrent().Name, FileSystemRights.FullControl, System.Security.AccessControl.AccessControlType.Allow);

            try
            {
                foreach (DirectoryInfo d in directoryInfo.GetDirectories())
                {
                    //d.GetAccessControl().ResetAccessRule(rule);
                    System.Diagnostics.Trace.WriteLine("currently reading&gt; " + d.FullName);
                    FindFiles(d, ref files);
                }

                files.AddRange(directoryInfo.GetFiles(string.Format("*.{0}", _selectedFileType), SearchOption.AllDirectories).Select(finfo =&gt; finfo.FullName));
            }
            catch (UnauthorizedAccessException excep)
            {
                System.Diagnostics.Trace.WriteLine(excep.Message);
            }
            catch (Exception e)
            {
                return; // MessageBox.Show(e.Message);
            }

        }

        private void PopulateResultList(Task searchedTask)
        {
            // fill up list view
            fileListView.Items.AddRange(files.Select(fileName =&gt; new ListViewItem { Checked = true, Text = fileName }).ToArray());
            toolStripStatusLabel.Text = string.Format("Total files found so far: {0}", files.Count);
        }

        private void btnZip_Click(object sender, EventArgs e)
        {

        }
    }
}
</code></pre>
