文件异步操作的实现
异步操作可以使我们的程序获得更优的性能,特别是对大文件进行处理时,这一点相信大家都知道。这里我写了一个示例。希望能给大家一个帮助。
using System.IO;
using System.Threading;
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ProgressBar progressBar1;
FileStream fs=null;
FileStream fsW=new FileStream("C://a.exe",FileMode.Create,FileAccess.Write,FileShare.Read);
int bufferLenth=1024*64;//将缓冲区定义为最少64k,在WIN平台上有很大的性能提高(性能分界线)
AsyncCallback cb=null;
byte[] bs=new byte[1024*64];
private System.Windows.Forms.TextBox textBox2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form2()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form2());
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(368, 40);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(136, 40);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(120, 48);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(232, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "E://tools//CHS_MSDE2000A.exe";
//上面这个文件可以换掉,最好大一点的文件.
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(72, 112);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(632, 40);
this.progressBar1.TabIndex = 2;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(56, 168);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox2.Size = new System.Drawing.Size(648, 272);
this.textBox2.TabIndex = 3;
this.textBox2.Text = "";
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(736, 485);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
}
#endregion
private void button1_Click(object sender, System.EventArgs e)
{
//这个文件最好大一点
string fName =this.textBox1.Text;
//使用异步读取。缓冲区最好大于64k.不然性能更低
fs=new FileStream(fName,FileMode.Open,FileAccess.Read,FileShare.ReadWrite,bufferLenth,true);
cb=new AsyncCallback(ShowText);
this.progressBar1.Maximum=(int)fs.Length;
fs.BeginRead(bs,0,bufferLenth,cb,fs);
//开始异步读取文件
}
//异步回调的实现函数,当缓冲区数据读完毕后
public void ShowText(IAsyncResult result)
{
int rb=fs.EndRead(result);//获取最终得到的数据量。不一定是64*1024。最后一次可能小于这个数
fsW.Write(bs,0,rb);//将读到的数据写入到别一个地方。
this.progressBar1.Value=(int)(fs.Position);//设置进度条
if(rb<bufferLenth) //如果当前读到的数据量小于缓冲区长度。则认为数据已全部读取
{
fsW.Flush();
fsW.Close();
fs.Close();
return;
}
fs.BeginRead(bs,0,bufferLenth,cb,fs);//如果还有数据再次读取64*1024字节
}