开始一个线程处理读取的文件并且更新到listbox中:
//处理数据: private void StartBrowser(string path) { UpdateFolderPath invoker = new UpdateFolderPath(DoUpdateFolder); this.Dispatcher.Invoke(invoker, path); UpdateFolder = new Thread(GetFiles); if (UpdateFolder.ThreadState == ThreadState.Running) { UpdateFolder.Abort(); } UpdateFolder.Start(); } protected void DoUpdateFolder(string path) { this.tbk_ForderPath.Text = path; this.folderPath = path; } private void GetFiles() { if (this.listItem.Count > 0) { this.listItem.RemoveAll(delegate(object s) { return s == null; }); } try { files = Directory.GetFiles(folderPath, "*.*", SearchOption.AllDirectories); foreach (string file in files) { FileInfo fi = new FileInfo(file); UpdateListUI update = new UpdateListUI(DoAddItem); this.Dispatcher.Invoke(update, fi); } } catch { System.Windows.MessageBox.Show("Access some files is denied. "); } } protected void DoAddItem(object item) { try { System.Drawing.Icon icon = Win32.GetIcon(item.ToString(), false); FileInfo fileInfo = item as FileInfo; DockPanel dp = new DockPanel(); System.Windows.Controls.Image img = new System.Windows.Controls.Image(); img.Height = 25; img.Width = 25; img.Source = icon.ToImageSource(); Run r1 = new Run(fileInfo.Name + "(" + fileInfo.Length + " Byte)"); Run r2 = new Run(fileInfo.FullName); TextBlock tbk1 = new TextBlock(); tbk1.Inlines.Add(r1); tbk1.Inlines.Add(new LineBreak()); tbk1.Inlines.Add(r2); dp.Children.Add(img); dp.Children.Add(tbk1); this.listbox1.Items.Add(dp); this.listbox1.ScrollIntoView(this.listbox1.Items[this.listbox1.Items.Count - 1]); this.listItem.Add(dp); this.pBar.Maximum = listItem.Count; } catch { } finally { } }