In general, the control operations on the form directly on the sub-thread are abnormal, which is due to the threads of the sub-threads and the running forms, so they want to operate the control on the form. It is impossible to operate through the control object name, but not to say that Microsoft provides Invoke method, its role is to let the sub-thread tell the window thread to complete the corresponding control operation.
Now use a process strip with thread control, the rough steps are as follows:
1. Create an Invoke function, which is approximately as follows:
///
/// delegate function to be invoked by main thread
/// summary>
Private void invokefun ()
{
PRGBAR.Value <100)
Prgbar.value = prgbar.value 1;
}
2. Subphone entry function:
///
/// Thread Function Interface
/// summary>
Private void threadfun ()
{
// Create Invoke Method by Specific Function
MethodInvoker Mi = New MethodInvoker (this.invokefun);
For (int i = 0; i <100; i )
{
THIS.BEGININVOKE (MI);
Thread.sleep (100);
}
}
3. Create a sub-thread:
Thread twprocess = new thread (New Threadstart (threadfun);
THDPROCESS.START ();
Remarks:
Using system.threading;
Private system.windows.Forms.ProgressBar prgbar;
The effect after run is as shown below: