Asynchronous operations allow our programs to achieve better performance, especially when handling big files, this is believed. Here I wrote an example. I hope to give you a help. using System.IO; using System.Threading; public class Form2: System.Windows.Forms.Form {private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.ProgressBar Progressbar1; filestream fs = null; filestream fsw = new filestream ("c: //a.exe", filemode.create, fileaccess.write, fileshare.read); int bufflenth = 1024 * 64; // Define the buffer as At least 64K, there is a large performance improvement (performance boundary line) on the WIN platform (Performance line) asyncCallback CB = NULL; BYTE [] BS = New Byte [1024 * 64]; private system.windows.Forms.TextBox textbox2; /// < Summary> // The required designer variable. /// summary> private system.componentmodel.container components = null;
Public Form2 () {// // Windows Form Designer Support for // InitializationComponent ();
// // Todo: Add any constructor code after INITIALIZEComponent call //}
///
#REGION Windows Form Designer The code ///
/// summary> private void InitializeComponent () {this.button1 = new System.Windows.Forms.Button (); this.textBox1 = new System.Windows.Forms.TextBox (); this.progressBar1 = new System. Windows.Forms.ProgressBar (); this.textbox2 = new system.windows.Forms.TextBox (); this.suspendlayout (); // // button1 // this.button1.location = new system.drawing.point (368 , 40); this.button1.name = "button1"; this.button1.size = new system.drawing.size (136, 40); this.button1.tabindex = 0; this.button1.text = "button1"; This.Button1.Click = new system.eventhandler (this.button1_click); // // textBox1 // this.textbox1.location = new system.drawing.point (120, 48); this.textbox1.name = "TextBox1 "; This.textBox1.size = new system.drawing.size (232, 21); this.textBox1.tabindex = 1; this.textBox1.text =" e: //toLS//chs_msde2000a.exe "; // above This file can be replaced, the best file. // // progressbar1 // this.progressbar1.location = new system.drawing.point (72, 112); this.prog RESSBAR1.NAME = "progravelbar1"; this.progressbar1.size = new system.drawing.size (632, 40); this.progressbar1.tabindex = 2; /// textBox2 // this.TextBox2.Location = new system. Drawing.Point (56, 168); this.textBox2.Multiline = true; this.textBox2.Name = "textBox2"; this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.textBox2.Size = new System.drawing.size (648, 272); this.textbox2.tabindex = 3; this.textBox2.text = ""; /// Form2 // this.doscalebasesize = new system.drawing.size (6, 14) ;
This.ClientSize = new system.drawing.size (736, 485); this.Controls.add (this.textBox2); this.controls.add (this.progressbar1); this.Controls.add (this.TextBox1); this. .Controls.add (this.button1); this.name = "form2"; this.text = "form2"; this.ResumeLayout (false);} #ENDREGION
Private void button1_click (object sender, system.eventargs e) {// This file is best big. String fname = this.TextBox1.text; // Use asynchronous read. 64k buffer is preferably greater than otherwise lower performance fs = new FileStream (fName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, bufferLenth, true);. Cb = new AsyncCallback (ShowText); this.progressBar1.Maximum = ( int) fs.length; fs.beginread (BS, 0, Bufferlenth, CB, FS); // Start asynchronous reading file
}
// Realization function of asynchronous callback, when the buffer data is read, public void showtext {INT RB = fs.endread (result); // Get the final data amount. It is not necessarily 64 * 1024. The last time may be less than this number fsw.write (BS, 0, RB); // writes the read data to a place. This.ProgressBar1.Value = (int); // Setting the progress bar if (RB