Generally, exceptions occur when you operate controls on the form directly in the Child thread. This is because the child thread and the thread running the form are different spaces, therefore, it is impossible for a child thread to operate controls on a form through the control object name, but it does not mean that operations cannot be performed. Microsoft providesInvokeThe sub-thread is used to tell the form thread to complete corresponding control operations.
Now we use a thread-controlled process bar to describe the following steps:
1.CreateInvokeFunction, which is roughly as follows:
///<Summary>
///Delegate function to be invoked by main thread
///</Summary>
Private VoidInvokefun ()
{
If(Maid. value <100)
Prgbar. value = prgbar. Value + 1;
}
2.Subthread entry function:
///<Summary>
///Thread function interface
///</Summary>
Private VoidThreadfun ()
{
// Create invoke method by specific function
Methodinvoker MI =NewMethodinvoker (This. Invokefun );
For(IntI = 0; I <100; I ++)
{
This. Begininvoke (MI );
Thread. Sleep (100 );
}
}
3.Create a subthread:
Thread thdprocess =NewThread (NewThreadstart (threadfun ));
Thdprocess. Start ();
Note:
UsingSystem. Threading;
PrivateSystem. Windows. Forms. progressbar prgbar;
How to display the progress bar in a mode window
Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 757351
I have read many people asking this question recently. I have also written an articleBlogThis section describes how to control the progress bar in a child thread. However, in most environments, a pop-up window is required to display the progress bar. You only need to make a slight modification on the basis of the previous settings.
First, it is the progress bar form. You need to add the progress bar above and remove it.Controlbox. In addition, you need to add a method to control the increase of the progress bar, as shown below:
///<Summary>
///Increase Process bar
///</Summary>
///<Param name = "nvalue">The value increased</Param>
///<Returns> </returns>
Public BoolIncrease (IntNvalue)
{
If(Nvalue> 0)
{
If(Prcbar. Value + nvalue <prcbar. Maximum)
{
Prcbar. Value + = nvalue;
Return True;
}
Else
{
Prcbar. value = prcbar. maximum;
This. Close ();
Return False;
}
}
Return False;
}
The next step is the main form. To perform operations, you must first define two private members and one delegate. One private member is to save the current progress bar form object, and the other is to save the delegate method (that is, to add the progress bar scale), as follows:
PrivateFrmprocpolicar myprocessbar =Null;
Private Delegate BoolIncreasehandle (IntNvalue );
PrivateIncreasehandle myincrease =Null;
Then provide a function in the main form to open the progress bar form, as shown below:
///<Summary>
///Open Process bar window
///</Summary>
Private VoidShowprocessbar ()
{
Myprocessbar =NewFrmprocessbar ();
// Init increase event
Myincrease =NewIncreasehandle (myprocessbar. increase );
Myprocessbar. showdialog ();
Myprocessbar =Null;
}
Now you can start to create a thread for running, as shown below:
///<Summary>
///Sub thread function
///</Summary>
Private VoidThreadfun ()
{
Methodinvoker MI =NewMethodinvoker (showprocessbar );
This. Begininvoke (MI );
Thread. Sleep (1000 );// Sleep a while to show window
BoolBlnincreased =False;
ObjectObjreturn =Null;
Do
{
Thread. Sleep (50 );
Objreturn =This. Invoke (This. Myincrease,
New Object[] {2 });
Blnincreased = (Bool) Objreturn;
}
While(Blnincreased );
}
note that when you open the progress bar form and add progress bars, begininvoke invoke , the difference here is begininvoke you do not need to wait for the method to finish running, invoke wait for the method to run. Another point is that the return value is used to determine whether the progress bar has reached the header. If you need other controls, you can use the preceding Method for expansion.
The startup thread can be as follows:
Thread thdsub =NewThread (NewThreadstart (threadfun ));
Thdsub. Start ();
In this way, the progress bar form is opened in the mode.