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 ...
随机推荐
- Mousetrap - Keyboard shortcuts in Javascript
Mousetrap is a simple library for handling keyboard shortcuts in Javascript. It is around 2kb minifi ...
- kettle菜鸟学习笔记2----第一个kettle转换的建立及执行
相关概念: Kettle数据清洗是采用元数据(Meta-data)驱动,以数据流的方式进行的,数据从数据源(数据库/文件等)在一系列相连的step之间依次向后流动,各个step完成对流经该step的数 ...
- Java之循环语句练习1
最近在猛复习Java,猛刷题目ing.这个做题目的过程其实也就像搬砖一样,一点一点把最基础的巩固好,一块一块.整整齐齐地砌才能砌好一面墙.好了,不说了,我要去搬砖了. 其实不瞒你们说,我是比较喜欢数学 ...
- 第十一篇、微信小程序-input组件
主要属性: 效果图: ml: <!--style的优先级比class高会覆盖和class相同属性--> <!--头像--> <view style="displ ...
- oracle 数据库导入导出
要把公司的数据库导入到自己的电脑上(都需要再命令窗口下输入指令) 导出数据库的基本代码: exp zj_user_kf/oracle@tzsw_4 file=d:\test3.dmp full=y 导 ...
- java大数--总结
BigInteger(高精度整数) 1.所在包: java.math.BigInteger 2.大数运算,以下返回类型均为BigInteger BigInteger a; BigInteger b; ...
- Linux读写锁的使用
读写锁是用来解决读者写者问题的,读操作可以共享,写操作是排它的,读可以有多个在读,写只有唯一个在写,写的时候不允许读. 具有强读者同步和强写者同步两种形式: 强读者同步:当写者没有进行写操作时,读者就 ...
- ☆RHEL6创建软raid的使用☆——经典之作
raid主要的种类 1.raid0 扩展卷 raid 0又称Stripee或Striping,中文译为集带工作方式, 有时也可以理解为拼凑. 它是将要存取的数据以条带状的形式尽量平均分配到多个硬 ...
- sql临时表和表变量
1. 为什么要使用表变量 表变量是从2000开始引入的,微软认为与本地临时表相比,表变量具有如下优点: a.与其他变量的定义一样,表变量具有良好的定义范围,并会被自动清除: b.在存储过程中使用表 ...
- js设计模式(9)---代理模式
0.前言 KG.PP被交易到了布鲁克林篮网,我的心情很复杂,一方面为他们不能终老celtics感到惋惜,另一方面为他们能够再次冲击总冠军感到高兴.从07年以来,作为一个铁杆celtics球迷,他们给我 ...