目录间复制文件的线程实现

本文介绍了三种不同的文件复制方法:普通方案、多线程方案和异步方案。普通方案是单线程同步复制,多线程方案通过创建新的线程来提高效率,而异步方案则利用委托的BeginInvoke和EndInvoke进行异步操作,允许在等待复制完成的同时执行其他任务。每种方案都有相应的进度条更新和状态显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
=============普通方案===================
private void button1_Click(object sender, EventArgs e)
{
DirectoryInfo dir = Directory.CreateDirectory("C://a");
DirectoryInfo dir2 = Directory.CreateDirectory("C://b");
FileOperator fileOperator = new FileOperator();
FileInfo[] files=dir.GetFiles();
this.progressBar1.Maximum = files.Length;
this.progressBar1.Step = 1;
foreach(FileInfo file in files )
{
this.label1.Text = "正在复制:" + file.FullName;
this.label1.Refresh();
fileOperator.CopyFile(file.FullName, dir2.FullName + "//" + file.Name);
this.progressBar1.Value++;
}
this.label1.Text = "复制完成,共手复制文件" + files.Length.ToString() + "个";
}


你的逻辑类
public class FileOperator
{
public void CopyFile(string file1, string file2)
{
//文件复制太快,为了看到效果,测试加上延迟,实际应用中去掉下面这行
System.Threading.Thread.Sleep(250);
File.Copy(file1, file2);
}
}

 

 

 

==========下面是多线程的方案=================

private void button1_Click(object sender, EventArgs e)
{
System.Threading.Thread t = new System.Threading.Thread(new ParameterizedThreadStart(this.StartCopy));
this.progressBar1.Step = 1;
t.Start(new string[] { "c://a", "c://b" });
}

private void StartCopy(object path)
{

string[] paths = (string[])path;
DirectoryInfo dir = Directory.CreateDirectory(paths[0]);
DirectoryInfo dir2 = Directory.CreateDirectory(paths[1]);
FileOperator fileOperator = new FileOperator();
FileInfo[] files=dir.GetFiles();
//由于当前线程不是界面控件创建的线程,不能直接操作,所以要使用委托方法回调操作控件。
this.progressBar1.Invoke(new MethodInvoker(delegate { this.progressBar1.Maximum = files.Length; }));
foreach(FileInfo file in files )
{
this.label1.Invoke(new MethodInvoker(delegate { this.label1.Text = "正在复制:" + file.FullName; }));
//this.label1.Refresh();
fileOperator.CopyFile(file.FullName, dir2.FullName + "//" + file.Name);
this.progressBar1.Invoke(new MethodInvoker(delegate { this.progressBar1.Value++; }));
}
this.label1.Invoke(new MethodInvoker(delegate { this.label1.Text = "复制完成,共手复制文件" + files.Length.ToString() + "个"; }));

}
}

public class FileOperator
{
public void CopyFile(string file1, string file2)
{
//文件复制太快,为了看到效果,测试加上延迟,实际应用中去掉下面这行
System.Threading.Thread.Sleep(250);
File.Copy(file1, file2);
}
}

 


========================下面是异步方案======================
定义一个委托
delegate void CopyHandler(string file1, string file1);
调用委托的BeginInvoke和EndInvoke
具体代码如下:

private void button1_Click(object sender, EventArgs e)
{
System.Threading.Thread t = new System.Threading.Thread(new ParameterizedThreadStart(this.StartCopy));
this.progressBar1.Step = 1;
t.Start(new string[] { "c://a", "c://b" });
}
//异步委托对象
delegate void CopyHandler(string file1, string file2);
private void StartCopy(object path)
{
string[] paths = (string[])path;
DirectoryInfo dir = Directory.CreateDirectory(paths[0]);
DirectoryInfo dir2 = Directory.CreateDirectory(paths[1]);
FileOperator fileOperator = new FileOperator();
FileInfo[] files = dir.GetFiles();
this.progressBar1.Invoke(new MethodInvoker(delegate { this.progressBar1.Maximum = files.Length; }));
CopyHandler copyHandler = new CopyHandler(fileOperator.CopyFile);
foreach (FileInfo file in files)
{

copyHandler.BeginInvoke(file.FullName, dir2.FullName + "//" + file.Name, new AsyncCallback(this.asyncCallBack), file.FullName);
//fileOperator.CopyFile(file.FullName, dir2.FullName + "//" + file.Name);

}
}

private void asyncCallBack(IAsyncResult result)
{
this.label1.Invoke(new MethodInvoker(delegate { this.label1.Text = "正在复制:" + result.AsyncState.ToString(); }));
AsyncResult r = (AsyncResult)result;
CopyHandler handler = (CopyHandler)r.AsyncDelegate;
handler.EndInvoke(result);
this.progressBar1.Invoke(new MethodInvoker(delegate { this.progressBar1.Value++; }));
}
}

public class FileOperator
{
public void CopyFile(string file1, string file2)
{
//文件复制太快,为了看到效果,测试加上延迟,实际应用中去掉下面这行
System.Threading.Thread.Sleep(250);
File.Copy(file1, file2);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值