C # Use of progress bars (example)

Source: Internet
Author: User

In the process of developing winfrom with C. Progress bars are often used to display progress information. At this time, we may need to use multiple threads. If we do not use multiple threads to control the progress bar, the window will easily be suspended (the progress information cannot be seen in due time ). Next I will give you an introduction based on an example I wrote.
First, let's take a look at the program interface.

Step 1: do not mention the design interface... note that you need to reference using system. Threading;
Step 2: Define a proxy to update the value of progressbar)

  1. // Update Progress list
  2. Private delegate void setpos (int ipos );

Step 3: update the progress bar value function (the parameter must be the same as the declared proxy parameter)

  1. Private void settextmessage (int ipos)
  2. {
  3. If (this. invokerequired)
  4. {
  5. Setpos = new setpos (settextmessage );
  6. This. Invoke (setpos, new object [] {IPOs });
  7. }
  8. Else
  9. {
  10. This. label1.text = IPOs. tostring () ++ "/100 ";
  11. This. progressbar1.value = convert. toint32 (IPOs );
  12. }
  13. }

Step 4: function implementation

  1. Private void button#click (Object sender, eventargs E)
  2. {
  3. Thread fthread = new thread (New threadstart (sleept); // open up a new thread
  4. Fthread. Start ();
  5. }

Step 5: The New thread executes the function:

  1. Private void sleept ()
  2. {
  3. For (INT I = 0; I <500; I ++)
  4. {
  5. System. Threading. thread. Sleep (100); // It is meaningless, with a simple execution latency.
  6. Settextmessage (100 * I/500 );
  7. }
  8. }

A simple progress bar program is ready. Simple. The rest depends on your own transformation. Please specify a source for reprinting. Http://blog.csdn.net/gisfarmerthank you.

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.