Using System. Threading; // Introduce namespace
Thread thread = Null ; // Second, declare a thread object
Public Delegate Void Delsetpro ( Int Pros ); // Declare a delegate object
/// / <Summary>
/// Set progress bar content
/// </Summary>
/// <Param name = "pros"> Gradient value </Param>
Private Void Setprocontent ( Int Pros)
{
If (Pros <= 100 )
{
// At first, the call is located in a thread other than the thread where the control is created. Therefore, the invoke method must be called.
If ( This . Invokerequired)
{
Delsetpro = New Delsetpro (setprocontent );
// On the thread that owns the control, run the specified delegate with the specified parameter list
This . Invoke (delsetpro, New Object [] {Pros });
}
Else
{
This . Pbarlinking. Value = Pros;
}
}
Else
{
Thread. Abort (); // Terminate thread
}
}
/// // <Summary>
/// Block current thread
/// </Summary>
Private Void Sleepthread ()
{
For ( Int I = 0 ; I < 500 ; I ++ )
{
Thread. Sleep ( 100 );
// Modify the progress speed of the progress bar. You can modify the number next to [/]. The smaller the number, the faster the speed.
Setprocontent ( 100 * I / 30 );
}
}
//Finally, start the thread in the form loading event or in the button clicking event.
Thread= NewThread (NewThreadstart (sleepthread ));
Thread. Start ();