using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace AsyncReaderFile
{
/// <summary>
/// Form1 的摘要描述。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.ProgressBar progressBar2;
private System.Windows.Forms.ProgressBar progressBar3;
/// <summary>
/// 設計工具所需的變數。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows Form 設計工具支援的必要項
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 呼叫之後加入任何建構函式程式碼
//
}
/// <summary>
/// 清除任何使用中的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form 設計工具產生的程式碼
/// <summary>
/// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改
/// 這個方法的內容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.progressBar2 = new System.Windows.Forms.ProgressBar();
this.progressBar3 = new System.Windows.Forms.ProgressBar();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 32);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 24);
this.button1.TabIndex = 0;
this.button1.Text = "讀取";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(16, 104);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 24);
this.button2.TabIndex = 1;
this.button2.Text = "讀取";
//
// button3
//
this.button3.Location = new System.Drawing.Point(16, 176);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 24);
this.button3.TabIndex = 2;
this.button3.Text = "讀取";
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(112, 32);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(320, 23);
this.progressBar1.TabIndex = 3;
//
// progressBar2
//
this.progressBar2.Location = new System.Drawing.Point(112, 104);
this.progressBar2.Name = "progressBar2";
this.progressBar2.Size = new System.Drawing.Size(320, 23);
this.progressBar2.TabIndex = 4;
//
// progressBar3
//
this.progressBar3.Location = new System.Drawing.Point(112, 176);
this.progressBar3.Name = "progressBar3";
this.progressBar3.Size = new System.Drawing.Size(320, 23);
this.progressBar3.TabIndex = 5;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
this.ClientSize = new System.Drawing.Size(464, 245);
this.Controls.Add(this.progressBar3);
this.Controls.Add(this.progressBar2);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 應用程式的主進入點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
public static void ReportReadingProgress(IAsyncResult asyncResult)
{
FileState stateObject = (FileState)asyncResult.AsyncState;
FileStream fs = (FileStream)asyncResult.AsyncState ;
int byteRed = fs.EndRead(asyncResult);
stateObject.currentPos +=byteRed;
stateObject.progressBar.Value = (int)(stateObject.currentPos *stateObject.progressBar.Maximum/stateObject.FileLenght);
if(stateObject.currentPos < stateObject.FileLenght )
{
AsyncCallback callBackMethod = new AsyncCallback (ReportReadingProgress);
fs.BeginRead(stateObject.readBuffer,0,(int)stateObject.bufferLenght,callBackMethod,stateObject);
}
else
{
MessageBox.Show(stateObject.FileName+"讀取結束","提示",MessageBoxButtons.OK,MessageBoxIcon.Information );
}
}
private void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog ofDlg = new OpenFileDialog ();
ofDlg.Filter = "All Files(*.*)|*.*";
if(ofDlg.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(ofDlg.FileName,FileMode.Open,FileAccess.Read,FileShare.Read,1,true);
FileState stateObject = new FileState();
stateObject.FileName = ofDlg.FileName;
stateObject.FileLenght = fs.Length;
stateObject.currentPos = 0;
stateObject.bufferLenght = 2000;
stateObject.readBuffer = new Byte[(int)stateObject.bufferLenght];
stateObject.progressBar = this.progressBar1;
AsyncCallback callBackMethod = new AsyncCallback(ReportReadingProgress);
fs.BeginRead(stateObject.readBuffer,0,(int)stateObject.FileLenght,callBackMethod,stateObject);
}
}
}
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、
FileState.cs //用戶自定義的數據訪問對象
using System;
namespace AsyncReaderFile
{
/// <summary>
/// FileState 的摘要描述。
/// </summary>
public class FileState
{
public string FileName;
public long FileLenght;
public byte[] readBuffer;
public long bufferLenght;
public long currentPos;
public System.Windows.Forms.ProgressBar progressBar;
public FileState()
{
//
// TODO: 在此加入建構函式的程式碼
//
}
}
}