Using system; using system.drawing; using system.collections; using system.componentmodel; using system.windows.form; using system.data; using system.io;
A summary description of the Namespace Tretest1 {Public Delegate Void ProgressDelegate (INT Progress); ///
Public Form2 () {// // Windows Form Designer Support for // InitializationComponent ();
// // Todo: Add any constructor code after INITIALIZEComponent call //}
///
#REGION Windows Form Designer The code ///
///
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 ("File is all read."); 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 = 0; while ((Read = stream.read (data, 0, 1024))! = 0) {TotalRead = read; if (progress! = null) {decimal x = TotalRead / FileLength; PROGRESS ((int) x);} thread.sleep (100);} if (onReadcomplete! = null) {onReadcomplete (this, evenetargs.empty);}}}}