Many developers see some software has a progress bar to show progress, they want to do, the project was built and found that it is not as simple as they imagined ... Read a lot of tutorials online, wrote a small demo for users to refer to ~~,demo's website: http://pan.baidu.com/s/1dDIxHvz
BackgroundWorker can be created in code or dragged in the toolbox, but if you want to use it, you must set its Workerreportsprogress property to true!
This demo is a copy of the file, in order to effect more obvious, it is recommended to copy more files, convenient progress bar display.
The interface is as follows:
Here's the whole code:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.IO;7 usingSystem.Linq;8 usingSystem.Text;9 usingSystem.Threading.Tasks;Ten usingSystem.Windows.Forms; One A namespaceDemo_progressbar - { - Public Partial classForm1:form the { - PublicForm1 () - { - InitializeComponent (); + } - + string[] FileNames =NULL;//Store file path A stringSavepath =NULL;//Store Save Path at - Private voidBtnchoosefiles_click (Objectsender, EventArgs e)//Add File to ListBox - { -OpenFileDialog o =NewOpenFileDialog ();//object that opens the file dialog box -O.multiselect =true;//files can be selected multiple - O.showdialog (); in if(O.filenames = =NULL)//If no files are selected - { to return; + } - Else the { *FileNames =O.filenames; $ for(inti =0; i < filenames.length; i++)Panax Notoginseng { - if(!lstbxshowfiles.items.contains (Filenames[i]))//added if the list does not contain elements the { + LstbxShowFiles.Items.Add (Filenames[i]); A } the } + } - } $ $ Private voidBtnchoosesavepath_click (Objectsender, EventArgs e)//Select Save Path - { -FolderBrowserDialog f =NewFolderBrowserDialog ();//Select Path the F.showdialog (); - if(F.selectedpath = =NULL)Wuyi { the return; - } Wu Else - { AboutTxtsavepath.text = Savepath =F.selectedpath; $ } - } - - Private voidbtnStart_Click (Objectsender, EventArgs e)//Start copying A { +PRGRSBR1. Maximum = LstbxShowFiles.Items.Count;//make the maximum value of the progress bar equal to the number of files to be copied in the list the for(inti =0; i < LstbxShowFiles.Items.Count; i++) - { $ using(FileStream fsread =NewFileStream (Filenames[i], FileMode.OpenOrCreate, FileAccess.Read))//Read file stream the { the using(FileStream fswrite =NewFileStream (Savepath +"\\"+ Path.getfilename (Filenames[i]), FileMode.OpenOrCreate, FileAccess.Write))//Write file stream the { the byte[] buffer =New byte[1]; - while(Fsread.read (Buffer,0, buffer. LENGTH)! =0)//a return value of zero means that you are finished reading in { theFswrite.write (Buffer,0, buffer. Length); the } About } the } theBckgrdwkrreport.reportprogress (i +1);//reporting progress, triggering progresschanged events the } +PRGRSBR1. Value =0; -MessageBox.Show ("Replication succeeded"); the lstbxShowFiles.Items.Clear ();Bayi txtsavepath.clear (); theFileNames =NULL; theSavepath =NULL; - } - the Private voidBckgrdwkrreport_progresschanged (Objectsender, ProgressChangedEventArgs e) the { thePRGRSBR1. Value = E.progresspercentage;//parameters passed by the E.progresspercentage:reportprogress method the } - } the}
The main use of BackgroundWorker is in the 60th line of the method. (Do not know the first two methods of self-Baidu ... Do not know using () and FileStream self-Baidu)
In 62 rows, we let the maximum value of the progress bar equal the number of files to be copied in the list, so that each file is copied in 76 rows with the ReportProgress method to report progress and trigger the BackgroundWorker progresschanged event. Through the arguments of the event E. Progresspercentage can get the parameters in the ReportProgress method.
Of course, BackgroundWorker has other methods and events that can be tried on its own, but generally only reportprogress methods and ProgressChanged events are used.
"WInform" using BackgroundWorker to control progress bar display progress