How to Use the backgroundworker backend display multi-threaded controls

Source: Internet
Author: User

Recently I went out to play and took a lot of photos. I wanted to upload them to all of you. I found that the photos were too big and I had a hard time uploading them.

So I wrote this batch photo shrinking tool and recalled the use of the backgroundworker background work control.

The interface is as follows. As follows:

(Original image)

(After downgrading)

 

 

 

Steps for using backgroundworker:

1. Drag a backgroundworker control.

 

2. Set its properties

Set the workerreportsprogress and workersuppscanscancellation attributes to true.

3. Generate Event code

Click the lightning button () and double-click the three event projects to generate three event codes.

4. compile background work events

Worker_dowork

1 private void worker_dowork (Object sender, doworkeventargs E) 2 {3 var Param = E. argument As zoomdata; 4 var photodir = new directoryinfo (Param. photodir); 5 var files = photodir. getfiles (); 6 var userstate = new reportdata () {All = files. count (), done = 0}; 7 worker. reportprogress (userstate. done, userstate); 8 foreach (var pic in files) 9 {10 try11 {12 userstate. done ++; 13 worker. reportprogress (userstate. done, userstate); 14 var IMG = image. fromfile (PIC. fullname); // if it is not an image, an error is reported here, so I do not need to analyze the 15 var width = Param. percent * IMG. width/100; 16 if (width <= 0) width = 1; 17 var Height = Param. percent * IMG. height/100; 18 if (height <= 0) Height = 1; 19 var newimage = IMG. getthumbnailimage (width, height, callback, intptr. zero); 20 newimage. save (path. combine (Param. afterdir, Pic. name); 21 IMG. dispose (); 22 newimage. dispose (); 23} 24 catch (system. exception ex) 25 {26 27} 28} 29}
Worker_progresschanged

1         private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)2         {3             var userState = e.UserState as ReportData;4             var value = e.ProgressPercentage * 100 / userState.all;5             if (value < pgbProcess.Minimum) value = pgbProcess.Minimum;6             if (value > pgbProcess.Maximum) value = pgbProcess.Maximum;7             pgbProcess.Value = value;8         }
Worker_runworkercompleted

1 private void worker_runworkercompleted (Object sender, runworkercompletedeventargs E) 2 {3 if (E. Error! = NULL) 4 {5 MessageBox. Show (E. Error. tostring (), "exception occurred"); 6 if (MessageBox. Show ("do you want to open the folder that saves the result? "," Prompt ", messageboxbuttons. yesno, messageboxicon. question) 7 = dialogresult. yes) 8 {9 process. start ("Explorer", txtafterdir. text); 10} 11} 12 else if (E. cancelled) 13 {14 if (MessageBox. show ("You have canceled the operation. Do you want to open the folder that saves the result? "," Prompt ", messageboxbuttons. yesno, messageboxicon. question) 15 = dialogresult. yes) 16 {17 process. start ("Explorer", txtafterdir. text); 18} 19} 20 else21 {22 if (MessageBox. show ("the operation has been completed. Do you want to open the folder that saves the result? "," Prompt ", messageboxbuttons. yesno, messageboxicon. question) 23 = dialogresult. yes) 24 {25 process. start ("Explorer", txtafterdir. text); 26} 27 pgbprocess. value = pgbprocess. minimum; 28 // lblprogress. TEXT = ""; 29 btnstart. TEXT = "start"; 30} 31}

This is a standard method of using the background work control, where worker_dowork is the method of background thread execution, using worker. the reportprogress (); method can stimulate the worker_progresschanged function. worker_progresschanged is executed in the UI thread and used to modify the progress bar. After the background thread finishes executing (or throwing an exception or canceling it in advance, the worker_runworkercompleted function is triggered to sort the completed tasks.

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.