WInform: Use BackgroundWorker to control the progress bar and display the progress bar.

Source: Internet
Author: User

WInform: Use BackgroundWorker to control the progress bar and display the progress bar.

Many developers see that some software has a progress bar showing the progress and want to figure it out. After the project is built, it is not as simple as they think... After reading many online tutorials, I wrote a small Demo for the reference of netizens ~~, Demo URL: http://pan.baidu.com/s/1dDIxHvz

BackgroundWorker can be created in the code or dragged in the toolbox. To use BackgroundWorker, you must set its WorkerReportsProgress attribute to True!

This Demo is a copy file. For more obvious effects, we recommend that you copy more files to facilitate the display of progress bars.

The interface is as follows:

The following is all the code:

1 using System; 2 using System. collections. generic; 3 using System. componentModel; 4 using System. data; 5 using System. drawing; 6 using System. IO; 7 using System. linq; 8 using System. text; 9 using System. threading. tasks; 10 using System. windows. forms; 11 12 namespace Demo_ProgressBar13 {14 public partial class Form1: Form15 {16 public Form1 () 17 {18 InitializeComponent (); 19} 20 21 string [] fileNames = Null; // storage file path 22 string savePath = null; // storage path 23 24 private void btnChooseFiles_Click (object sender, EventArgs e) // Add the file to listBox25 {26 OpenFileDialog o = new OpenFileDialog (); // object 27 o in the open file dialog box. multiselect = true; // You can select 28 o for multiple files. showDialog (); 29 if (o. fileNames = null) // if no file is selected 30 {31 return; 32} 33 else34 {35 fileNames = o. fileNames; 36 for (int I = 0; I <fileNames. length; I ++) 37 {38 if (! LstbxShowFiles. items. contains (fileNames [I]) // Add 39 {40 lstbxShowFiles if the list does not contain elements. items. add (fileNames [I]); 41} 42} 43} 44} 45 46 private void btnChooseSavePath_Click (object sender, EventArgs e) // select the Save path 47 {48 FolderBrowserDialog f = new FolderBrowserDialog (); // select the path 49 f. showDialog (); 50 if (f. selectedPath = null) 51 {52 return; 53} 54 else55 {56 txtSavePath. text = savePath = f. selectedPath; 57} 58} 59 60 Private void btnStart_Click (object sender, EventArgs e) // Start copying 61 {62 prgrsbr1.Maximum = lstbxShowFiles. items. count; // make the maximum value of the progress bar equal to the number of objects to be copied in the list 63 for (int I = 0; I <lstbxShowFiles. items. count; I ++) 64 {65 using (FileStream fsRead = new FileStream (fileNames [I], FileMode. openOrCreate, FileAccess. read) // Read the file stream 66 {67 using (FileStream fsWrite = new FileStream (savePath + "\" + Path. getFileName (fileNames [I] ), FileMode. openOrCreate, FileAccess. write) // Write the file stream 68 {69 byte [] buffer = new byte [1]; 70 while (fsRead. read (buffer, 0, buffer. length )! = 0) // If the returned value is zero, it indicates that the system has finished reading 71 {72 fsWrite. write (buffer, 0, buffer. length); 73} 74} 75} 76 bckgrdwkrReport. reportProgress (I + 1); // report progress, triggering ProgressChanged event 77} 78 prgrsbr1.Value = 0; 79 MessageBox. show ("copied"); 80 lstbxShowFiles. items. clear (); 81 txtSavePath. clear (); 82 fileNames = null; 83 savePath = null; 84} 85 86 private void bckgrdwkrReport_ProgressChanged (object sender, ProgressChangedEventArgs e) 87 {88 prgrsbr1.Value = e. progressPercentage; // e. progressPercentage: parameters passed by the ReportProgress method 89} 90} 91}

This method is mainly used when the BackgroundWorker is in the 60th rows. (I don't know the first two methods of Baidu... I do not know using () or FileStream's Baidu)

 

In row 62, we set the maximum value of the progress bar to be equal to the number of files to be copied in the list, in this way, every time a file is copied, The ReportProgress method is used to report the progress and trigger the BackgroundWorker ProgressChanged event on line 76. The event parameter e. progressPercentage can be used to obtain parameters in the ReportProgress method.

 

Of course, there are other methods and events in the BackgroundWorker. You can try them on your own, but generally only use the ReportProgress method and ProgressChanged event.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.