Displays the code of the progress bar when reading the file (9CBS)

xiaoxiao2021-03-06  41

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

/// Form1. /// public class form2: system.windows.Forms.form {private system.windows.forms.progressbar programs.Forms.Windows.Forms.Button Button1; /// /// Designer variable. /// private system.componentmodel.container components = null;

Public Form2 () {// // Windows Form Designer Support for // InitializationComponent ();

// // Todo: Add any constructor code after INITIALIZEComponent call //}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {components.dispose ();}} Base.Dispose

#REGION Windows Form Designer The code ///

/// designer supports the required method - do not use the code editor to modify the // / this method. /// 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.progressb Ar1); this.name = "form2"; this.text = "form2"; this.load = new system.eventhandler (this.form2_load); this.ResumeLayout (false);} #ENDREGION

///

/// The primary entry point of the application. /// [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 ("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);}}}}

转载请注明原文地址:https://www.9cbs.com/read-71625.html

New Post(0)