Ejemplo n.º 1
0
        private void GetListViewItems(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = (BackgroundWorker)sender;
            Stopwatch        watch  = new Stopwatch();
            ShellItem        parent = e.Argument as ShellItem;

            watch.Reset();
            watch.Start();
            List <ShellItem> subFolders = parent.GetFolders(false);
            List <ShellItem> subFiles   = parent.GetFiles();

            watch.Stop();
            int foldersCount = subFolders.Count;
            int filesCount   = subFiles.Count;

            ListViewItem[] files   = new ListViewItem[filesCount];
            ListViewItem[] folders = new ListViewItem[foldersCount];

            if (foldersCount + filesCount > 0)
            {
                mre.WaitOne();
                Thread t1   = null;
                Thread t2   = null;
                Thread t3   = null;
                Thread t4   = null;
                bool   slow = watch.ElapsedMilliseconds > 800;
                if (slow)
                {
                    int quarter = filesCount >> 2;
                    int half    = filesCount >> 1;
                    t1 = new Thread(delegate() { LoadListViewItems(files, subFiles, 0, quarter); });
                    t2 = new Thread(delegate() { LoadListViewItems(files, subFiles, quarter, half); });
                    t3 = new Thread(delegate() { LoadListViewItems(files, subFiles, half, half + quarter); });
                    t4 = new Thread(delegate() { LoadListViewItems(files, subFiles, half + quarter, filesCount); });
                    t1.SetApartmentState(ApartmentState.MTA);
                    t2.SetApartmentState(ApartmentState.MTA);
                    t3.SetApartmentState(ApartmentState.MTA);
                    t4.SetApartmentState(ApartmentState.MTA);
                    t1.Start();
                    t2.Start();
                    t3.Start();
                    t4.Start();
                    LoadListViewItems(folders, subFolders, 0, foldersCount);
                }
                else
                {
                    t1 = new Thread(delegate() { LoadListViewItems(files, subFiles, 0, filesCount); });
                    t1.Start();
                    LoadListViewItems(folders, subFolders, 0, foldersCount);
                }
                Array.Sort(folders, sorter);
                Items.AddRange(folders);

                if (slow)
                {
                    t1.Join();
                    t2.Join();
                    t3.Join();
                    t4.Join();
                }
                else
                {
                    t1.Join();
                }
            }
            worker.ReportProgress(100, files);
        }