开始一个线程处理读取的文件并且更新到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)

04-23 02:45