开始一个线程处理读取的文件并且更新到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. 第一个过滤器Filter

    过滤器实现Filter接口javax.servlet.Filter package com.henau.example; import java.io.IOException; import java ...

  2. jsonp跨越请求百度搜索api 实现下拉列表提示

    题目来源: 最近在做百度IFE前端技术学院的题,然后有一题就是模拟百度搜索智能提示.题目是开源的,稍后给出地址. 因为博主没学过后端啊,欲哭无泪,所以不能实现后端模糊搜索,那如果前端ajax纯粹请求一 ...

  3. 运用DataTable进行行转列操作

    public DataTable GetReverseTable(DataTable p_Table) { DataTable _Table = new DataTable(); ; i != p_T ...

  4. table-layout:fixed 属性的解说

    table-layout:fixed 属性的解说如果想要一个table固定大小,里面的文字强制换行(尤其是在一长串英文文本,并且中间无空格分隔的情况下),以达到使过长的文字不撑破表格的目的,一般是使用 ...

  5. Android之图片颜色调节

    package net.hnjdzy.imagecolor; import android.os.Bundle; import android.app.Activity; import android ...

  6. 从数据库里面取值绑定到Ztree

    1.效果图(思路:将数据库表按照一定的格式排序,然后序列化成json字符串,绑定到Ztree上并显示出来) zTree v3.5.16 API 文档 :http://www.ztree.me/v3/a ...

  7. jQuery简单邮箱验证

    function chekmail() { var szReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; ...

  8. 使用MSBUILD 构建时出错 error MSB3086: Task could not find "sgen.exe" using the SdkToolsPath的解决方法

    如果项目有添加有WB引用,比如引用其它网站的WEB服务等,那么VS在编译时会自动生成个 [项目名称].Serializers.dll的文件,就是把引用服务中的相关对象信息生成硬编码的程序集,以提高效率 ...

  9. 关于柯尼卡美能达bizhub250出现c2557错误解决方法

    打印机出现c2557代码的操作方法 1.         按效用/计数器键 英文是(Utility/Counter) 2.         看到有检查细息(Check Detail) 3.       ...

  10. ORACLE-树状数据结构获取各层级节点信息

    平时工作中出报表时,要求分别列出员工的一级部门,二级部门....,在数据库中,部门表(unit)的设计一般为在表中维护每个部门的上级部门(pid字段),或者通过一个关联表(unit_link)维护层级 ...