void ExplorerTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e) { Cursor oldCursor = this.Cursor; ShellItem item = (ShellItem)e.Node.Tag; ShellItem shRoot = (ShellItem)root.Tag; Cursor = Cursors.WaitCursor; Enabled = false; if (!item.Equals(shRoot)) { if (e.Node.Nodes.Count == 1 && e.Node.Nodes[0].Text.Equals(" : ")) { e.Node.Nodes.Clear(); List <ShellItem> subDirs = item.GetFolders(true); TreeNode[] nodes = new TreeNode[subDirs.Count]; for (int i = 0; i < subDirs.Count; i++) { nodes[i] = MakeNode(subDirs[i]); } e.Node.Nodes.AddRange(nodes); } else { RefreshNode(e.Node); } } Enabled = true; Cursor = oldCursor; }
private void LoadRoot() { ShellItem desktop = new ShellItem(); List <ShellItem> subDirs; root = new TreeNode(desktop.Text); root.ImageIndex = imageList.GetIconIndex(desktop, false); root.SelectedImageIndex = imageList.GetIconIndex(desktop, true); root.Tag = desktop; subDirs = desktop.GetFolders(true); foreach (ShellItem sh in subDirs) { root.Nodes.Add(MakeNode(sh)); } Nodes.Clear(); Nodes.Add(root); root.Expand(); }
public void RefreshNode(TreeNode node) { ShellItem nodeItem = (ShellItem)node.Tag; if (!(node.Nodes.Count == 1 && node.Nodes[0].Text.Equals(" : "))) { if (nodeItem.RefreshFolders()) { List <ShellItem> curDirs = new List <ShellItem>(); curDirs.AddRange(nodeItem.GetFolders(false)); List <TreeNode> invalidNodes = new List <TreeNode>(); bool match; foreach (TreeNode tn in node.Nodes) { match = false; for (int i = 0; i < curDirs.Count; i++) { if (curDirs[i].Equals(tn.Tag)) { curDirs.RemoveAt(i); match = true; break; } } if (!match) { invalidNodes.Add(tn); } } if (invalidNodes.Count + curDirs.Count > 0) { try { BeginUpdate(); TreeViewNodeSorter = null; TreeNode[] nodes = new TreeNode[curDirs.Count]; int i = 0; foreach (TreeNode tn in invalidNodes) { node.Nodes.Remove(tn); } foreach (ShellItem sh in curDirs) { nodes[i] = MakeNode(sh); i++; } node.Nodes.AddRange(nodes); TreeViewNodeSorter = sorter; } catch (Exception e) { Debug.WriteLine(e.Message); } finally { EndUpdate(); } } } } }
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); }