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

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);
   }

  }
 }
}


 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值