一段讀取文件時顯示進度條的代碼(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);
   }

  }
 }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章