您的位置:首页 > 编程语言

一段读取文件时显示进度条的代码(CSDN上收录)

2005-01-08 13:06 423 查看
using System;
using System.Threading;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace treetest1
{
public delegate void ProgressDelegate(int progress);

/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form2 : System.Windows.Forms.Form
{
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form2()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <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.progressBar1 = new System.Windows.Forms.ProgressBar();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// progressBar1
//
this.progressBar1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.progressBar1.Location = new System.Drawing.Point(0, 69);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(472, 16);
this.progressBar1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 24);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(472, 85);
this.Controls.Add(this.button1);
this.Controls.Add(this.progressBar1);
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form2());
}

private void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog dia = new OpenFileDialog();
if(dia.ShowDialog() == DialogResult.OK)
{
ReadFileThread thread = new ReadFileThread();
thread.fileName = dia.FileName;
thread.Progress += new ProgressDelegate(UpdateProgressBar);
thread.OnReadComplete += new EventHandler(OnReadComplete);
Thread t = new Thread(new ThreadStart(thread.ReadFile));
t.IsBackground = true;
t.Start();
}
}
public void UpdateProgressBar(int progress)
{
if(InvokeRequired)
{
ProgressDelegate pd = new ProgressDelegate(UpdateProgressBar);
Invoke(pd,new object[]{progress});
}
else
{
progressBar1.Value = progress;
}
}

private void OnReadComplete(object sender, EventArgs e)
{
MessageBox.Show("文件全部读取完毕。");
progressBar1.Value = 0;
}

private void Form2_Load(object sender, System.EventArgs e)
{

}
}
public class ReadFileThread
{
public ProgressDelegate Progress;
public event EventHandler OnReadComplete;
internal string fileName;
private decimal fileLength;
private decimal totalRead;
private byte[] data;
public void ReadFile()
{
FileInfo fi = new FileInfo(fileName);
fileLength = fi.Length;
FileStream stream = new FileStream(fileName,FileMode.Open,FileAccess.Read,FileShare.Read,4096);
if(data == null)
{
data = new byte[1024];
}
int read = 0;
while((read=stream.Read(data,0,1024)) != 0)
{
totalRead += read;
if(Progress != null)
{

decimal x = (totalRead / fileLength) * 100;
Progress((int)x);
}
Thread.Sleep(100);
}
if(OnReadComplete != null)
{
OnReadComplete(this,EventArgs.Empty);
}

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