Invoke and BeginInvoke in C #

Source: Internet
Author: User

Original posts: http://www.cnblogs.com/Z-King/archive/2011/11/03/2234337.html

has been the use of invoke and BeginInvoke and the concept of confusion, the two days to read some information, the use of these two and the principle of a new understanding and understanding.

First of all, there are two scenarios for invoke and BeginInvoke use:

1. Invoke, BeginInvoke in control.

2. Invoke, BeginInvoke in delegate.

These two situations are different, and we're going to talk about the 1th kind here. Here we are speaking. The official definition of invoke and BeginInvoke in net.

Control.Invoke (Parameter delegate) method: executes the specified delegate on the thread that owns the underlying window handle of this control.

Control.BeginInvoke (Parameter delegate) method: Executes the specified delegate asynchronously on the thread that creates the control's underlying handle.

Based on these two concepts we generally understand that the invoke table is synchronous , BeginInvoke represents asynchronous . But how do you synchronize and async? Let's do a test.

Invoke Example:

private void Button1_Click (object sender, EventArgs e)
{
MessageBox.Show (Thread.CurrentThread.GetHashCode (). ToString () + "AAA");
Invokethread = new Thread (new ThreadStart (Startmethod));
Invokethread.start ();
String a = string. Empty;
for (int i = 0; i < 3; i++)//adjust cycle times to see more clearly
{
Thread.Sleep (1000);
A = a + "B";
}
MessageBox.Show (Thread.CurrentThread.GetHashCode (). ToString () +a);
}

private void Startmethod ()
{
MessageBox.Show (Thread.CurrentThread.GetHashCode (). ToString () + "CCC");
Button1. Invoke (New Invokedelegate (InvokeMethod));
MessageBox.Show (Thread.CurrentThread.GetHashCode (). ToString () + "DDD");
}

private void InvokeMethod ()
{
Thread.Sleep (3000);
MessageBox.Show (Thread.CurrentThread.GetHashCode (). ToString () + "EEE");
}

Conclusion: After we run, we look at the sequence of running the program, 1AAA->3CCC and 1bbb->1eee->3ddd.

Explanation: The main thread runs 1AAA, then 1BBB and the child thread 3CCC execute simultaneously, then through invoke to submit the InvokeMethod method to the main thread, then the child thread waits for the main thread to execute, Until the main thread completes the InvokeMethod method (the task of the main thread must wait for the task to complete before it executes the Invoke commit), and finally executes the child thread 3DDD.

BeginInvoke Example:

private void Button1_Click (object sender, EventArgs e)
{
MessageBox.Show (Thread.CurrentThread.GetHashCode (). ToString () + "AAA");
Invokethread = new Thread (new ThreadStart (Startmethod));
Invokethread.start ();
String a = string. Empty;
for (int i = 0; i < 3; i++)//adjust cycle times to see more clearly
{
Thread.Sleep (1000);
A = a + "B";
}
MessageBox.Show (Thread.CurrentThread.GetHashCode (). ToString () +a);
}

private void Startmethod ()
{
MessageBox.Show (Thread.CurrentThread.GetHashCode (). ToString () + "CCC");
Button1. BeginInvoke (New Invokedelegate (InvokeMethod));
MessageBox.Show (Thread.CurrentThread.GetHashCode (). ToString () + "DDD");
}

private void Begininvokemethod ()
{
Thread.Sleep (3000);
MessageBox.Show (Thread.CurrentThread.GetHashCode (). ToString () + "eeeeeeeeeeee");
}

Conclusion: After we run we look at the results of the execution: 1AAA->1BBB and 3ccc->1eee and 3DDD.

Explanation: The main thread runs 1AAA, then 1BBB and the child thread 3CCC are executed simultaneously, then the InvokeMethod method is submitted to the main thread via BeginInvoke, then the main thread executes 1EEE (the main thread performs its own task execution), while the child threads continue to perform 3DDD.

With this two-piece code test comparison, we will find that both the invoke and BeginInvoke commit methods are executed in the main thread, in fact, according to my invoke and BeginInvoke definition we want to see this problem in the sub-thread, In the invoke example, we will find that the delegate method submitted by invoke is completed before the execution of DDD can continue, and in the BeginInvoke example we will find that the BeginInvoke commits the delegate method, and the child threads continue to execute DDD without waiting for the delegate method to complete. So now we are recalling the concept of Invoke (synchronous) and BeginInvoke (asynchronous), in fact, they say the meaning is relative to the child thread, in fact, the call to the control is always executed by the main thread. Many of us do not understand this synchronous and asynchronous, mainly because we chose the wrong reference. In fact, sometimes the concept of light is easy to understand the wrong.

Invoke and BeginInvoke in C #

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.