SoFunction
Updated on 2025-03-08

Summary of the difference between Invoke and BeginInvoke in C#

Introduction to this article: When BeginInvoke() is called, the current thread will enable a thread in the thread pool to execute this method. The current thread is not blocked and continues to run the subsequent code. When Invoke() is called, the current thread will be blocked and the subsequent code will be continued until the Invoke() method returns. The execution of one delegate among these two methods.

1. The understanding between c# Invoke and BeginInvoke

1. When Invoke() is called, Invoke will prevent the running of the current main thread. It will not continue to execute the subsequent code until the Invoke() method returns, showing the concept of "synchronization".

2. When BeginInvoke() is called, the current thread will enable a thread in the thread pool to execute this method. BeginInvoke will not prevent the running of the current main thread, but will wait until the current main thread has done things before executing the code content in BeginInvoke, showing the concept of "asynchronous".

3. EndInvoke() . When you want to get the result after BeginInvoke() is executed, call this method to get it.

2. Explain the difference between c# Invoke and BeginInvoke through examples

1. Example

 private void button1_Click(object sender, EventArgs e)     
     {
  this. = "1";
         (new EventHandler(delegate       {
            this. += "2";
         }));
         this. += "3";
      }

The result is: 123

2. Example

  private void button1_Click(object sender, EventArgs e)     
     {
  this. = "1";
         (new EventHandler(delegate       {
            this. += "2";
         }));
         this. += "3";
      }

The result is: 132

3. Example

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
namespace InvokeTest
{
    delegate void MsgDelegate(String str);//Declare a proxy    public partial class Form1 : Form
    {
        Thread[] threads = new Thread[10];//If you do not use the keyword to new the object, an error "The object reference is not set to the instance of the object." will be reported.        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
             = "Main thread";
            for(int i=0;i<10;i++)
            {  Thread ts = new Thread(new ThreadStart(threadProc));   = "Thread group member"+()+"Number";  threads[i] = ts;    = true;  ();  }
        }
        private void showMsg(String str)
        {
            (5000);//The thread sleeps for 5 seconds...            (str+"It's in"++"Executed");//Show which thread is executing the proxy function        }
        private void button1_Click(object sender, EventArgs e)
        {
            ("Execute A1 code snippet");
            Thread T1 = new Thread(threadProc1);
             = "Thread One";
            ();
            ("Execute B1 code snippet");
        }
        private void button2_Click(object sender, EventArgs e)
        {
            ("Execute A2 code snippet");
            Thread T2 = new Thread(threadProc2);
             = "Thread Two";
            ();
            ("Execute B2 code snippet");
        }
        /// <summary>
        /// Thread function of thread one        /// </summary>
        private void threadProc1()
        {
            Invoke(new MsgDelegate(showMsg), new object[] { "Call showMsg through Invoke" });//Equivalent to the next statement that takes a long time to call the code and starts to execute after the next statement is finished.            ("The Invoke function is called after it. Because Invoke is executed synchronously, I have been waiting for showMsg to be executed just now, alas...");
        }
        /// <summary>
        /// Thread function of thread two        /// </summary>
        private void threadProc2()
        {
            BeginInvoke(new MsgDelegate(showMsg), new object[] { "Call showMsg through BeginInvoke" });//It is equivalent to executing the next statement immediately after calling the code that takes a long time.            ("BeginInvoke function is called, but due to the asynchronous execution of BeginInvoke, I was executed before the showMsg was executed! Haha...");
        }
        private void threadProc()
        {
            (+"It's already running!");
        }
    }
}

This is the end of this article about the difference between Invoke and BeginInvoke in C#. For more related content on the difference between Invoke and BeginInvoke, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!