C # winform use progress bar (two forms)

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.

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. PrivateDelegateVoidSetpos (IntIPOs );

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

  1. PrivateVoidSettextmessage (IntIPOs)
  2. {
  3. If(This. Invokerequired)
  4. {
  5. Setpos =NewSetpos (settextmessage );
  6. This. Invoke (setpos,NewObject[] {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. PrivateVoidButton#click (ObjectSender, eventargs E)
    2. {
    3. Thread fthread =NewThread (NewThreadstart (sleept ));// Open up a new thread
    4. Fthread. Start ();
    5. }

Step 5: The New thread executes the function:

  1. PrivateVoidSleept ()
  2. {
  3. For(IntI = 0; I <500; I ++)
  4. {
  5. System. Threading. thread. Sleep (100 );// There is no meaning, simple execution latency
  6. Settextmessage (100 * I/500 );
  7. }
  8. }

 

==================================

Use floating progress bar

1. Floating window

/// <Summary>
/// Increase process bar
/// </Summary>
/// <Param name = "nvalue"> the value increased </param>
/// <Returns> </returns>
Public bool increase (INT nvalue)
{

If (nvalue> 0)
{< br> If (prcbar. value + nvalue {< br> prcbar. value + = nvalue;
return true;
}

Else
{
Prcbar. value = prcbar. maximum;
This. Close ();
Return false;
}

}
Return false;
}

 

2. Progress window

Using system. Threading;

Private frmprocpolicar myprocessbar = NULL;
Private delegate bool increasehandle (INT nvalue );
Private increasehandle myincrease = NULL;

 

Private void showprocessbar ()
{
Myprocessbar = new frmprocessbar ();
// Init increase event
Myincrease = new increasehandle (myprocessbar. increase );
Myprocessbar. startposition = formstartposition. centerscreen;
Myprocessbar. showdialog ();
Myprocessbar = NULL;

}
Private void threadfun ()
{

Methodinvoker MI = new methodinvoker (showprocessbar );
This. begininvoke (MI );
Thread. Sleep (1000); // sleep a while to show window
Bool blnincreased = false;
Object objreturn = NULL;
Do
{
Thread. Sleep (50 );
Objreturn = This. Invoke (this. myincrease, new object [] {2 });
Blnincreased = (bool) objreturn;
}
While (blnincreased );
}

 

Usage:
Thread thdsub = new thread (New threadstart (threadfun ));
Thdsub. Start ();

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.