C # The functions and usage of this. Invoke,

Source: Internet
Author: User

C # The functions and usage of this. Invoke,

Invoke () is used to execute the specified delegate on the main thread of the application. General application: when modifying the attributes of objects in the UI thread (main thread) in the auxiliary thread, call this. Invoke ();

In multi-threaded programming, we often need to update the interface display in the working thread. In multithreading, it is wrong to call interface controls directly, invoke and BeginInvoke occur to solve this problem, so that you can safely update the interface display in multiple threads.

The correct method is to encapsulate the code that involves updating the interface in the working thread as a method and call it through Invoke or BeginInvoke. The difference between the two is that one causes the worker thread to wait, while the other does not. (Reference: http://lidunyang2008.blog.163.com/blog/static/190676205201322325426803)
The so-called "one-side response operation, one-side node addition" can always be relative, so that the burden on the UI thread is not too great, because correct interface updates must always be done through the UI thread, what we need to do is to wrap most of the operations in the work thread, and put the pure interface updates into the UI thread for the purpose of reducing the burden on the UI thread. This. invoke () usage:

(1)

// Modify the Enabled attribute of the button

Private void ModifyButton (bool _ B)

{

This. Button1.Enabled = _ B;

}

(2)

// Declare the delegate of the above method

Private delegate void ModifyButton_dg (bool _ B );

(3)

// Call the delegate

Private void Calldelgate ()

{

/* Use this. Invoke in Windows Forms applications and use this. Dispatcher. Invoke in WPF Applications */

This. Invoke (new ModifyButton_dg (ModifyButton), new object [] {false });

}

(4)

It can be called in a non-UI thread, for example:
// Create a thread

Thread _ t = new Thread (new ThreadStart (threadmethod ));
_ T. Start ();

// Thread entry

Private void threadmethod ()
{
// Other Code omitted
Calldelgate ();
}

The thread will switch between the UI thread and the auxiliary thread

 

For example:

For example, when you start a thread and want to update a TextBox in the form in the thread method ..

Using System. Threading;

// Start a thread
Thread thread = new Thread (new ThreadStart (DoWork ));
Thread. Start ();

// Thread method
Private void DoWork ()
{
This. TextBox1.Text = "I Am a text box";/* The method for directly calling interface controls in multiple threads is incorrect */

}

If you operate like above, there will be exceptions in VS2005 or 2008...

The correct method is to use Invoke \ BeginInvoke

Using System. Threading;
Namespace test
{
Public partial class Form1: Form
{

 

Public Form1 ()
{
InitializeComponent ();
}

 

① // Update the TextBox1 display content in the form

Public void UpdateForm (string param1, string parm2)
{
This. textBox1.Text = param1 + parm2;
}

 

② // Declare Delegation

 

 

 

Public delegate void UpdateForm_dl (string str1, string str2 );

③ // Call the delegate

Private void Calldelegate ()

{

/* Use this. Invoke in Windows Forms applications and use this. Dispatcher. Invoke in WPF Applications */

This. BeginInvoke (new UpdateForm_dl (UpdateForm), new object [] {"I Am a text box", "haha "});

// This. Dispatcher. BeginInvoke (new UpdateForm_dl (UpdateForm), new object [] {"I Am a text box", "haha "});

}


④ // Create a new thread

Private void button#click (object sender, EventArgs e)
{
Thread thread = new Thread (new ThreadStart (DoWork ));
Thread. Start ();
}

 

⑤ // New thread entry

Public void DoWork ()

{
Calldelegate ();
}


}
}
Pay attention to the use of proxy!

Related Article

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.