您的位置:首页 > 其它

WPF调用线程(二)复制文件并显示进度条

2011-04-03 22:42 585 查看
这一段时间要进行WPF及多线程的培训,于是就写了一个例子,主要功能是用复制文件时,显示进度条。以演示在WPF中,如何调用线程,基础理论就不多说了,园子里好多大牛都写过,MSDN也有详尽介绍,也可以查看我的前两篇文章,


C#线程基础

WPF调用线程(-)

也有一些介绍,先看运行效果

后台CS内容

namespace WpfThreadTest
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
Thread timeThread;
Thread copyThread;
public MainWindow()
{
InitializeComponent();
this.displayTimeByThread.Text = DateTime.Now.ToLocalTime().ToString("yyyy年MM月dd日 hh:mm:ss"); ;
timeThread = new Thread(new ThreadStart(DispatcherThread));
}
private void button3_Click(object sender, RoutedEventArgs e)
{
timeThread.Start();
}
public void DispatcherThread()
{
//可以通过循环条件来控制UI的更新
while (true)
{
///线程优先级,最长超时时间,方法委托(无参方法)
displayTimeByThread.Dispatcher.BeginInvoke(
DispatcherPriority.Normal, new Action(UpdateTime));
Thread.Sleep(1000);
}
}

private void UpdateTime()
{
this.displayTimeByThread.Text = DateTime.Now.ToLocalTime().ToString("yyyy年MM月dd日 hh:mm:ss");
}

private void Window_Closed(object sender, EventArgs e)
{
///关闭所有启动的线程
timeThread.Abort();
copyThread.Abort();
Application.Current.Shutdown();
}

private void button1_Click(object sender, RoutedEventArgs e)
{
///设定要复制的文件全路径
OpenFileDialog openFile = new OpenFileDialog();
openFile.AddExtension = true;
openFile.CheckPathExists = true;
openFile.Filter = "*.rar|*.rar|all files|*.*";
openFile.FilterIndex = 0;
openFile.Multiselect = false;
bool? f=openFile.ShowDialog();
if (f!=null && f.Value)
{
this.srcFile.Text = openFile.FileName;
}
}

private void button2_Click(object sender, RoutedEventArgs e)
{
///设定目标文件全路径
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.AddExtension = true;
saveFile.Filter = "*.rar|*.rar|all files|*.*";
saveFile.FilterIndex = 0;

bool? f= saveFile.ShowDialog();
if (f != null && f.Value)
{
this.saveFilePath.Text = saveFile.FileName;
}
}

private void button4_Click(object sender, RoutedEventArgs e)
{
string fileName=this.srcFile.Text.Trim();
string destPath=this.saveFilePath.Text.Trim();
if(!File.Exists(fileName))
{
MessageBox.Show("源文件不存在");
return;
}

///copy file and nodify ui that rate of progress of file copy
this.copyFlag.Text = "开始复制。。。";

//设置进度条最大值,这句代码写的有点郁闷
this.copyProgress.Maximum = (new FileInfo(fileName)).Length;

//保存复制文件信息,以进行传递
CopyFileInfo c = new CopyFileInfo() { SourcePath = fileName, DestPath = destPath };
//线程异步调用复制文件
copyThread = new Thread(new ParameterizedThreadStart(CopyFile));
copyThread.Start(c);

this.copyFlag.Text = "复制完成。。。";

}
/// <summary>
/// 复制文件的委托方法
/// </summary>
/// <param name="obj">复制文件的信息</param>
private void CopyFile(object obj)
{
CopyFileInfo c = obj as CopyFileInfo;
CopyFile(c.SourcePath, c.DestPath);
}
/// <summary>
/// 复制文件
/// </summary>
/// <param name="sourcePath"></param>
/// <param name="destPath"></param>
private void CopyFile( string sourcePath,string destPath)
{
FileInfo f = new FileInfo(sourcePath);
FileStream fsR = f.OpenRead();
FileStream fsW = File.Create(destPath);
long fileLength = f.Length;
byte[] buffer = new byte[1024];
int n = 0;

while (true)
{
///设定线程优先级
///异步调用UpdateCopyProgress方法
///并传递2个long类型参数fileLength 与 fsR.Position
this.displayCopyInfo.Dispatcher.BeginInvoke(DispatcherPriority.SystemIdle,
new Action<long, long>(UpdateCopyProgress), fileLength, fsR.Position);

//读写文件
n=fsR.Read(buffer, 0, 1024);
if (n==0)
{
break;
}
fsW.Write(buffer, 0, n);
fsW.Flush();
Thread.Sleep(1);
}
fsR.Close();
fsW.Close();
}

private void UpdateCopyProgress(long fileLength,long currentLength)
{
this.displayCopyInfo.Text = string.Format("总大小:{0},已复制:{1}", fileLength, currentLength);
//刷新进度条
this.copyProgress.Value = currentLength;
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{

}

}
public class CopyFileInfo
{
public string SourcePath { get; set; }
public string DestPath { get; set; }
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: