Using System.ComponentModel
Direct use of the Egprogressbar method
BackgroundWorker Bw =NewBackgroundWorker (); ListView ListView1=NewListView (); Public voidEgprogressbar () {DirectoryInfo di=NewDirectoryInfo (@"c:/windows"); Fileinfo[] fi=di. GetFiles (); Progressbar1.maximum= fi. Length-1;//set the maximum value of the progress barBw.workersupportscancellation =true; Bw.workerreportsprogress=true; Bw.dowork+=NewDoworkeventhandler (ADD);//Binding EventsBw.progresschanged + =NewProgresschangedeventhandler (Progress); Bw.runworkercompleted+=NewRunworkercompletedeventhandler (END); Bw.runworkerasync (); } Public voidADD (Objectsender, DoWorkEventArgs e) {DirectoryInfo di=NewDirectoryInfo (@"c:/windows"); foreach(FileInfo Iteminchdi. GetFiles ()) {ListViewItem LVT=NewListViewItem ();//use ListViewItem to store information that gets to a fileLvt. Text =item. FullName; Lvt. SubItems.Add (DateTime.Now.ToLongTimeString ()); Thread.Sleep ( -); Bw.reportprogress (ListView1.Items.Count, LVT);//report The current progress, get to the first few files and the file information } } Public voidProgress (Objectsender, ProgressChangedEventArgs e) {progressBar1.Value= E.progresspercentage;//get the first few files to change the progress of the progress barListViewItem LV = e.userstate asListViewItem; LISTVIEW1.ITEMS.ADD (LV);//Add the newly acquired file information to the ListView } Public voidEnd (Objectsender, Runworkercompletedeventargs e) {progressBar1.Value=0;//progress bar Clear 0}
Pass-through method parameter RunWorkerAsync (parameter)
Get the e.argument of method DoWorkEventArgs
At the end of the decision DoWork method E.result automatically passed to completed E.result
"Go" "C # Use the background to dynamically load data" Winform "anti-interface card dead" "BackgroundWorker" class