现在的位置: 首页 > 综合 > 正文

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

2014年02月26日 ⁄ 综合 ⁄ 共 3457字 ⁄ 字号 评论关闭

 
=============普通方案===================
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);
}
}

抱歉!评论已关闭.