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 > )
{
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)的更多相关文章
- WPF 多线程处理(1)
WPF 多线程处理(1) WPF 多线程处理(2) WPF 多线程处理(3) WPF 多线程处理(4) WPF 多线程处理(5) WPF 多线程处理(6) 废话不多说,先上图: 多线程处理数据后在th ...
- WPF 多线程处理(5)
WPF 多线程处理(1) WPF 多线程处理(2) WPF 多线程处理(3) WPF 多线程处理(4) WPF 多线程处理(5) WPF 多线程处理(6) 项目的目录: 以下是FileStroage的 ...
- WPF 多线程处理(6)
WPF 多线程处理(1) WPF 多线程处理(2) WPF 多线程处理(3) WPF 多线程处理(4) WPF 多线程处理(5) WPF 多线程处理(6) 以下是子窗体的UI: <Window ...
- WPF 多线程处理(3)
WPF 多线程处理(1) WPF 多线程处理(2) WPF 多线程处理(3) WPF 多线程处理(4) WPF 多线程处理(5) WPF 多线程处理(6) 首先我们需要几个属性来保存取得的数据,因为在 ...
- WPF 多线程处理(2)
WPF 多线程处理(1) WPF 多线程处理(2) WPF 多线程处理(3) WPF 多线程处理(4) WPF 多线程处理(5) WPF 多线程处理(6) WPF UI 设计需要自动适应窗体大小,那么 ...
- 用 UI 多线程处理 WPF 大量渲染的解决方案
众所周知, WPF 的 UI 渲染是单线程的,所以如果我们异步或者新建线程去进行数据处理的时候,处理完,想要更新 UI 的时候,需要调用一下 Dispatcher.Invoke,将处理完的数据推入到 ...
- C# & WPF 随手小记之一 ——初探async await 实现多线程处理
嗯...我也是在园子待了不短时间的人了,一直以来汲取着园友的知识,感觉需要回馈什么. 于是以后有空我都会把一些小技巧小知识写下来,有时候可能会很短甚至很简单,但希望能帮到大家咯. 第一篇文章来说说as ...
- Git使用总结 Asp.net生命周期与Http协议 托管代码与非托管代码的区别 通过IEnumerable接口遍历数据 依赖注入与控制反转 C#多线程——优先级 AutoFac容器初步 C#特性详解 C#特性详解 WPF 可触摸移动的ScrollViewer控件 .NET(C#)能开发出什么样的APP?盘点那些通过Smobiler开发的移动应用
一,原理 首先,我们要明白Git是什么,它是一个管理工具或软件,用来管理什么的呢?当然是在软件开发过程中管理软件或者文件的不同版本的工具,一些作家也可以用这个管理自己创作的文本文件,由Linus开发的 ...
- 在WPF中使用依赖注入的方式创建视图
在WPF中使用依赖注入的方式创建视图 0x00 问题的产生 互联网时代桌面开发真是越来越少了,很多应用都转到了浏览器端和移动智能终端,相应的软件开发上的新技术应用到桌面开发的文章也很少.我之前主要做W ...
随机推荐
- 数组去重算法,quickSort
function removeRepeat(arr) { var arr2 = [] ,obj = {}; for (var i = 0; i<arr.length; i++) { var nu ...
- 504 Gateway Time-out 和 502 Bad Gateway相关处理
若报:504 Gateway Time-out则与nginx有关 解决方案: #vim nginx.conf 添加以下代码: http{ fastcgi_connect_timeout 300; fa ...
- Set集合——HashSet、TreeSet、LinkedHashSet(2015年07月06日)
一.Set集合不同于List的是: Set不允许重复 Set是无序集合 Set没有下标索引,所以对Set的遍历要通过迭代器Iterator 二.HashSet 1.HashSet由一个哈希表支持,内部 ...
- Table of Contents - ActiveMQ
Getting Started ActiveMQ 的安装 Hello World Configuring Standard ActiveMQ Components Connecting to Acti ...
- js完美解决IE6不支持position:fixed的bug
详细内容请点击 <!DOCTYPE html><html><head><meta http-equiv="Content-Type" co ...
- 跟我一起学习ASP.NET 4.5 MVC4.0(六)(转)
这一系列文章跨度有点大,由于最近忙于其他事情,没有更新,今天重新安装了下Win8系统,VS2012和SQLServer 2012,顺便抽空继续一篇.随着VS2012 RC版本的放出,ASP.NET M ...
- 删除utorrent广告栏
自从utorrent附带广告栏后整个页面很难看,特别是右面下载详细列表变得支离破碎,今天实在是忍不了,网络搜索关键字“remove utorrent ads”,有效解决了问题. 具体如下:打开设置,选 ...
- 网站seo新手快速提升自己的技巧
第一.找自身的问题 大多数从业者都有下面两个严重的问题: 1.过于放大SEO的重要性每个人,都有自大的习惯,地位越NB往往越把自己认知的一切当做真理,其实有可能那只是井口那巴掌大的一片天.在网络营销中 ...
- 获取字符串对应的MD5值 (AL16UTF16LE)
CREATE OR REPLACE FUNCTION fn_md5_utf16le (InputString IN VARCHAR2) RETURN VARCHAR2 IS retval ); /** ...
- C语言知识总结(3)
数组 数组的特点: 只能存放一种类型的数据,比如int类型的数组.float类型的数组 里面存放的数据称为“元素” 初始化方式 ] = {, , }; ] = {,}; , , }; ] = {[]= ...