Use control. invoke to return the current window thread

Source: Internet
Author: User

VS2008, C #3.0
In WinForm development, we usually do not want the form to be stuck when a button is clicked on the form to execute a service until the service is executed. The most direct method is multithreading. Multi-threaded programming is essential in WinForm development.
This article describes how to use multithreading in WinForm development and how to use the Control. Invoke method in the thread to return the form main thread for related operations.

 

-. WinForm multi-thread programming
1. new Thread ()
Open a new thread and execute a method without passing parameters:

Private void DoWork (){
Thread t = new Thread (new ThreadStart (this. DoSomething ));
T. Start ();
}
Private void DoSomething (){
MessageBox. Show ("thread start ");
}

Open a new thread, execute a method, and pass the parameters:

Private void DoWork (){
Thread t = new Thread (new ParameterizedThreadStart (this. DoSomething ));
T. Start ("guozhijian ");
}
Private void DoSomething (object o ){
MessageBox. Show (o. ToString ());
}
The parameter is defined as the object type.
2. ThreadPool
As we all know, the cost of opening a new thread is very high. If we open a new thread for each operation, it is too wasteful. Therefore, the thread pool is used below.
No parameter transfer:

Private void DoWork (){
ThreadPool. QueueUserWorkItem (new WaitCallback (this. DoSomething ));
}
Private void DoSomething (object o ){
MessageBox. Show ("thread start ");
}
Parameter passing:

Private void DoWork (){
ThreadPool. QueueUserWorkItem (new WaitCallback (this. DoSomething), "guozhijian ");
}
Private void DoSomething (object o ){
MessageBox. Show (o. ToString ());
}
The anonymous method is more flexible:

Private void DoWork (){
String name = "guozhijian ";
ThreadPool. QueueUserWorkItem (new WaitCallback (delegate (object o ){
MessageBox. Show (name );
}));
}
You can directly access local variables in anonymous code segments without worrying about parameter passing issues.
Ii. Invoke
1. this. Invoke
Now, after the execution is completed in the business thread, you need to change the value of the Form Control. At this time, if you get the control handle directly through this, then the operation on it will throw an exception ,. net WinForm Application does not allow such operations. This is. You can call the Invoke method.

2. Invoke method signature:
Object Control. Invoke (Delegate Method)
Object Control. Invoke (Delegate Method, params object [] args)

3. Use custom Delegation
Private void DoWork (){
WaitCallback wc = new WaitCallback (this. DoSomething );
ThreadPool. QueueUserWorkItem (wc, "Guozhijian ");
}

Private delegate void MyInvokeDelegate (string name );
Private void DoSomething (object o ){
This. Invoke (new MyInvokeDelegate (this. ChangeText), o. ToString ());
}

Private void ChangeText (string name ){
This. textBox1.Text = name;
}
Oh, it's too troublesome. Do I have to define a delegate every time.

4. Use System. Action:
Private void DoWork (){
WaitCallback wc = new WaitCallback (this. DoSomething );
ThreadPool. QueueUserWorkItem (wc, "Guozhijian ");
}

Private void DoSomething (object o ){
This. Invoke (new Action <string> (this. ChangeText), o. ToString ());
}

Private void ChangeText (string name ){
This. textBox1.Text = name;
}
In this example, a parameter, System. action has many overload values, and can have no parameters (non-generic), but can have up to four parameters. Similarly, the anonymous method is used instead of the generic System. action:
Private void DoWork (){
WaitCallback wc = new WaitCallback (this. DoSomething );
ThreadPool. QueueUserWorkItem (wc, "Guozhijian ");
}

Private void DoSomething (object o ){
This. Invoke (new Action (delegate (){
This. textBox1.Text = o. ToString ();
}));
}

5. Use System. Func
After Invoke calls the main form operation, you also want to get a return value after the call:
Private void DoWork (){
WaitCallback wc = new WaitCallback (this. DoSomething );
ThreadPool. QueueUserWorkItem (wc, "Guozhijian ");
}

Private void DoSomething (object o ){
System. Func <string, int> f = new Func <string, int> (this. GetId );
Object result = this. Invoke (f, o. ToString ());
MessageBox. Show (result. ToString ());
}

Private int GetId (string name ){
This. textBox1.Text = name;
If (name = "Guozhijian "){
Return 999;
}
Else {
Return 0;
}
}

The value of result is 999.
System. Func also has a lot of wildcard overloading, which is not described here.

6. About Invoke owner: Control
In this example, this is used for reference. Here, this is used to replace the handle of any Control in the form with OK, because Control. invoke means to delegate the method to the thread with the Control for execution.

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.