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)
- // Update Progress list
- 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)
- Private void settextmessage (int ipos)
- {
- If (this. invokerequired)
- {
- Setpos = new setpos (settextmessage );
- This. Invoke (setpos, new object [] {IPOs });
- }
- Else
- {
- This. label1.text = IPOs. tostring () ++ "/100 ";
- This. progressbar1.value = convert. toint32 (IPOs );
- }
- }
Step 4: function implementation
- Private void button#click (Object sender, eventargs E)
- {
- Thread fthread = new thread (New threadstart (sleept); // open up a new thread
- Fthread. Start ();
- }
Step 5: The New thread executes the function:
- Private void sleept ()
- {
- For (INT I = 0; I <500; I ++)
- {
- System. Threading. thread. Sleep (100); // It is meaningless, with a simple execution latency.
- Settextmessage (100 * I/500 );
- }
- }
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.