[C # Use the backend to dynamically load data] winform "defense against brute-force attacks"

Source: Internet
Author: User

Original article address:

Http://www.cnblogs.com/Zeech-Lee/archive/2011/10/14/2212376.html

 

 

Nonsense: recently encountered projects found that when winform obtains a large amount of data, the interface is suspended and the experience is very poor. As a tester, we have the obligation and responsibility to raise bugs. Every test is faced with an interface that has been stuck for more than five seconds. It is really a kind of torture and a kind of suicide. You can use multiple threads to process data, or use the background to obtain data. Multi-threading can solve the problem, but it may be more complicated to dynamically load data. Because of multithreading, You need to execute a thread to update the data. The intelligence is not high enough. When using the background, you can report the obtained data at any time, which can be precise to a specific item, and it is easy to use, because it has been encapsulated.

Class used: backgroundworker under using system. componentmodel;

This class has four important methods: dowork, progresschanged, runworkercompleted, runworkerasync, two important attributes workersuppscanscancellation, workerreportsprogress

Method:

1. dowork: indicates what to do with the background. When binding time, the input parameter is the method name.

2. progresschanged: What should I do in the background when the progress changes, and the parameter passed in when the binding time is the method name

3. runworkercompleted

4. runworkerasync: enables backend and starts running in the background.

Attribute:

1. workersuppscanscancellation: whether asynchronous cancellation is supported

2. workerreportsprogress: whether to report progress updates. This attribute is very important and must be enabled to report progress.

The following example shows how to dynamically load data in the background:

Let's first make a simple interface

The required function is: Click start to start listing the file names under drive C one by one, and the progress bar shows the progress.

The first method is to add the file name to the listview. In this way, thread. Sleep (500), Sleep for 0.5 seconds:

View code

 1    Public  Void Add ( Object Sender, doworkeventargs E)
2 {
3 Directoryinfo di = New Directoryinfo ( @" C :/ " );
4 Foreach (Fileinfo item In Di. getfiles ())
5 {
6 Listviewitem lvt = New Listviewitem (); // use listviewitem to store the information of the obtained object.
7 Lvt. Text = item. fullname;
8 Lvt. subitems. Add (datetime. Now. tolongtimestring ());
9 Thread. Sleep ( 500 );
10 Bw. reportprogress (listview1.items. Count, lvt); // report the current progress and obtain the information of the file and the file.
11 }
12
13 }

Method 2: report the progress after obtaining the progress:

View code

 1    Public   Void Progress ( Object Sender, progresschangedeventargs E)
2 {
3 Progressbar1.value = E. progresspercentage; // Obtain the objects to change the progress of the progress bar.
4 Listviewitem Lv = E. userstate As Listviewitem;
5 Listview1.items. Add (LV ); // Add the latest file information to listview
6 }

The third method looks like a chicken in this example. The event is executed after the background is completed:

 
1Public VoidEnd (ObjectSender, asynccompletedeventargs E)
2{
3Progressbar1.value =0;//Progress bar cleared 0
4 }

Finally, write the following method in the double-click event:

View code

1     Private   Void Button#click ( Object Sender, eventargs E)
2 {
3 Directoryinfo di = New Directoryinfo ( @" C :/ " );
4 Fileinfo [] Fi = Di. getfiles ();
5 Progressbar1.maximum = Fi. Length- 1 ; // Set the maximum value of the progress bar
6 Bw. workersuppscanscancellation = True ;
7 Bw. workerreportsprogress = True ;
8 Bw. dowork + =New Doworkeventhandler (ADD ); // Bind event
9 Bw. progresschanged + = New Progresschangedeventhandler (Progress );
10 Bw. runworkercompleted + = New Runworkercompletedeventhandler (end );
11 Bw. runworkerasync ();
12 }

Note: When defining three methods in the background, the format must be a template.

You can run the following results. Obviously, this is what the user needs. If you want to retrieve large data volumes at a time or operate them in a single thread, the experience is very poor.

If software is to be recognized, the user experience plays a very important role. Although a software uses a very advanced technology, the user experience is very poor, users often weigh the next choice

More convenient.

:Http://files.cnblogs.com/Zeech-Lee/%E5%90%8E%E5%8F%B0%E6%93%8D%E4%BD%9C.rar

If you like me, follow me!

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.