开始一个线程处理读取的文件并且更新到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 > )
{
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 = ;
img.Width = ;
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 - ]); 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();
}

更新进度条的值:

        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+;
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 > )
{
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 = ;
img.Width = ;
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 - ]); 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();
} 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+;
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)

WPF 多线程处理(4)的更多相关文章

  1. WPF 多线程处理(1)

    WPF 多线程处理(1) WPF 多线程处理(2) WPF 多线程处理(3) WPF 多线程处理(4) WPF 多线程处理(5) WPF 多线程处理(6) 废话不多说,先上图: 多线程处理数据后在th ...

  2. WPF 多线程处理(5)

    WPF 多线程处理(1) WPF 多线程处理(2) WPF 多线程处理(3) WPF 多线程处理(4) WPF 多线程处理(5) WPF 多线程处理(6) 项目的目录: 以下是FileStroage的 ...

  3. WPF 多线程处理(6)

    WPF 多线程处理(1) WPF 多线程处理(2) WPF 多线程处理(3) WPF 多线程处理(4) WPF 多线程处理(5) WPF 多线程处理(6) 以下是子窗体的UI: <Window ...

  4. WPF 多线程处理(3)

    WPF 多线程处理(1) WPF 多线程处理(2) WPF 多线程处理(3) WPF 多线程处理(4) WPF 多线程处理(5) WPF 多线程处理(6) 首先我们需要几个属性来保存取得的数据,因为在 ...

  5. WPF 多线程处理(2)

    WPF 多线程处理(1) WPF 多线程处理(2) WPF 多线程处理(3) WPF 多线程处理(4) WPF 多线程处理(5) WPF 多线程处理(6) WPF UI 设计需要自动适应窗体大小,那么 ...

  6. 用 UI 多线程处理 WPF 大量渲染的解决方案

    众所周知, WPF 的 UI 渲染是单线程的,所以如果我们异步或者新建线程去进行数据处理的时候,处理完,想要更新 UI 的时候,需要调用一下 Dispatcher.Invoke,将处理完的数据推入到 ...

  7. C# & WPF 随手小记之一 ——初探async await 实现多线程处理

    嗯...我也是在园子待了不短时间的人了,一直以来汲取着园友的知识,感觉需要回馈什么. 于是以后有空我都会把一些小技巧小知识写下来,有时候可能会很短甚至很简单,但希望能帮到大家咯. 第一篇文章来说说as ...

  8. Git使用总结 Asp.net生命周期与Http协议 托管代码与非托管代码的区别 通过IEnumerable接口遍历数据 依赖注入与控制反转 C#多线程——优先级 AutoFac容器初步 C#特性详解 C#特性详解 WPF 可触摸移动的ScrollViewer控件 .NET(C#)能开发出什么样的APP?盘点那些通过Smobiler开发的移动应用

    一,原理 首先,我们要明白Git是什么,它是一个管理工具或软件,用来管理什么的呢?当然是在软件开发过程中管理软件或者文件的不同版本的工具,一些作家也可以用这个管理自己创作的文本文件,由Linus开发的 ...

  9. 在WPF中使用依赖注入的方式创建视图

    在WPF中使用依赖注入的方式创建视图 0x00 问题的产生 互联网时代桌面开发真是越来越少了,很多应用都转到了浏览器端和移动智能终端,相应的软件开发上的新技术应用到桌面开发的文章也很少.我之前主要做W ...

随机推荐

  1. JavaScript--正则表达式(笔记)

    一 什么是正则表达式 // 正则表达式(regular expression)是一个描述字符模式的对象; // JS定义RegExp类表示正则表达式; // String和RegExp都定义了使用正则 ...

  2. 学习Slim Framework for PHP v3 (四)--get()是怎么加进去的?

    看看官网加粗的一句话: At its core, Slim is a dispatcher that receives an HTTP request, invokes an appropriate ...

  3. DWZ LookUp Suggest 教程

    单个查找带回 jsp 代码 lookup.jsp <%@ page language="java" contentType="text/html; charset= ...

  4. yarn.resourcemanager.ha.id设置

    resourcemanager启动报错,其中一个启动成功,另一个启动报8088端口被成功启动的rm占用 2016-11-18 17:08:49,478 INFO org.apache.zookeepe ...

  5. mysql数据库用户和权限管理记录

    一.MySQL用户的基本说明: 1.1 用户的基本结构MySQL的用户:用户名@主机 ■用户名:16个字符以内■主机:可以是主机名.IP地址.网络地址等主机名:www.111cn.net,localh ...

  6. Server.UrlEncode、HttpUtility.UrlDecode的区别

    Server.UrlEncode.HttpUtility.UrlDecode的区别 在对URL进行编码时,该用哪一个?这两都使用上有什么区别吗? 测试: string file="文件上(传 ...

  7. UI1_UITableViewHomeWork

    // // AppDelegate.m // UI1_UITableViewHomeWork // // Created by zhangxueming on 15/7/14. // Copyrigh ...

  8. 移动Web轮播图IOS卡顿的问题

    晚饭前,被测试吐槽说,banner轮播手动左右滑的时候会卡顿.我一看不科学啊,大水果手机怎么会卡顿.我一看测试手中拿的是iPod,我觉得大概是这小玩意性能不强悍,后来又拿来5S,依然会卡顿,有趣的是, ...

  9. 胸腺嘧啶“T”

    4.下列物质或结构中含胸腺嘧啶“T”的是( )A.DNA聚合酶 B.烟草花叶病毒C.ATP D.B淋巴细胞中的线粒体

  10. cocos2d-x 节点操作 -->J2ME

      cocos2d-x 的节点操作涉及到以下几点          1.  节点之间的关系          2.  节点的添加操作          3.  节点的删除操作          4.  ...