WPF 多线程处理(4)
开始一个线程处理读取的文件并且更新到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 { } }
开一个一个线程处理读取的数据传到子窗体:

private void DoWork() { UpdateListUI update = new UpdateListUI(DoUpdateItem); foreach (object item in listItem) { this.Dispatcher.Invoke(update, item); } } protected void StopWork() { if (UpdateList != null) UpdateList.Abort(); if (UpdateFolder != null) UpdateFolder.Abort(); if (UpdatePBar != null) UpdatePBar.Abort(); Environment.Exit(1); }
更新进度条的值:

protected void DoUpdateItem(object item) { var index=listItem.FindIndex( delegate(object i) { return i==item; }); //this.listbox1.SelectedIndex = index; this.pBar.Visibility = Visibility.Visible; this.pBar.Value = index+1; if (pBar.Value==pBar.Maximum) { this.pBar.Visibility = Visibility.Hidden; } }
将Icon转化成ImageSource
这个是Icon的扩展方法:

internal static class IconUtilities { [DllImport("gdi32.dll", SetLastError = true)] private static extern bool DeleteObject(IntPtr hObject); public static ImageSource ToImageSource(this Icon icon) { Bitmap bitmap = icon.ToBitmap(); IntPtr hBitmap = bitmap.GetHbitmap(); ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap( hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); if (!DeleteObject(hBitmap)) { throw new Win32Exception(); } return wpfBitmap; } }
以下是全部代码:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Threading; using System.IO; using System.Windows.Forms; using Automatically.FileStroage; using System.Runtime.InteropServices; using System.Drawing; using System.Windows.Interop; using System.ComponentModel; namespace Automatically { /// <summary> /// Interaction logic for Main.xaml /// </summary> public partial class MainWindow : Window { private string folderPath; private string[] files; private List<object> listItem; private Thread UpdateList = null; private Thread UpdateFolder = null; private Thread UpdatePBar = null; private delegate void UpdateListUI(object ob); private delegate void UpdateFolderPath(string path); private delegate void UpdatePBarUI(object obj); public MainWindow() { listItem = new List<object>(); InitializeComponent(); this.MouseDown+=(s,e)=>{ if (e.LeftButton == MouseButtonState.Pressed) { this.Cursor = System.Windows.Input.Cursors.Cross; this.DragMove(); } }; this.MouseUp += (s, e) => { this.Cursor = System.Windows.Input.Cursors.Arrow; }; btn_Top.Click += (s, e) => { if (this.Topmost == true) { this.Topmost = false; } else { this.Topmost = true; } }; btn_Min.Click += (s, e) => { this.WindowState = WindowState.Minimized; }; btn_Close.Click += (s, e) => { this.Close(); }; btn_Broswer.Click += (s, e) => { FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd.RootFolder = System.Environment.SpecialFolder.DesktopDirectory; fbd.ShowDialog(); StartBrowser(fbd.SelectedPath); }; btn_Start.Click += (s, e) => { UpdateList = new Thread(DoWork); if (UpdateList.ThreadState == ThreadState.Running) { UpdateList.Abort(); } UpdateList.Start(); }; this.listbox1.SelectionChanged += (s, e) => { var lbi = this.listbox1.SelectedItem; View view = new View(lbi); view.ShowDialog(); }; this.Closing += (s, e) => { StopWork(); }; } //处理数据: 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 { } } private void DoWork() { UpdateListUI update = new UpdateListUI(DoUpdateItem); foreach (object item in listItem) { this.Dispatcher.Invoke(update, item); } } protected void StopWork() { if (UpdateList != null) UpdateList.Abort(); if (UpdateFolder != null) UpdateFolder.Abort(); if (UpdatePBar != null) UpdatePBar.Abort(); Environment.Exit(1); } protected void DoUpdateItem(object item) { var index=listItem.FindIndex( delegate(object i) { return i==item; }); //this.listbox1.SelectedIndex = index; this.pBar.Visibility = Visibility.Visible; this.pBar.Value = index+1; if (pBar.Value==pBar.Maximum) { this.pBar.Visibility = Visibility.Hidden; } } } internal static class IconUtilities { [DllImport("gdi32.dll", SetLastError = true)] private static extern bool DeleteObject(IntPtr hObject); public static ImageSource ToImageSource(this Icon icon) { Bitmap bitmap = icon.ToBitmap(); IntPtr hBitmap = bitmap.GetHbitmap(); ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap( hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); if (!DeleteObject(hBitmap)) { throw new Win32Exception(); } return wpfBitmap; } } }
下一篇:WPF 多线程处理(5)
上一篇:WPF 多线程处理(3)
作者:风清扬 No.1
出处:http://www.cnblogs.com/fengqingyangNo1
如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】按钮。
如果,您希望更容易地发现我的新博客,不妨点击一下右下角的 【关注 风清扬 No.1】。
因为,我的写作热情也离不开您的肯定支持。
感谢您的阅读,如果您对我的博客所讲述的内容有兴趣,请继续关注我的后续博客.
出处:http://www.cnblogs.com/fengqingyangNo1
如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】按钮。
如果,您希望更容易地发现我的新博客,不妨点击一下右下角的 【关注 风清扬 No.1】。
因为,我的写作热情也离不开您的肯定支持。
感谢您的阅读,如果您对我的博客所讲述的内容有兴趣,请继续关注我的后续博客.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 单线程的Redis速度为什么快?
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库