C# About Invoke (detailed explanation)

C# About Invoke

First of all, there are two situations for the use of invoke and begininvoke:

  1. Invoke, begininvoke in control.
  2. invoke, begininvoke in delegate. These two situations are different, and what we are going to talk about here is the first one. Let's talk about the official definitions of invoke and begininvoke in .NET.

control.invoke(parameter delegate) method: Execute the specified delegate on the thread that owns the underlying window handle of this control.

control.begininvoke (parameter delegate) method: execute the specified delegate asynchronously on the thread where the basic handle of the control is created.

The meaning of invoke is: to execute the specified delegate synchronously on the presentation of the underlying window handle that owns the control (synchronous) The
meaning of beginInvoke is: to execute the delegate asynchronously on the thread where the underlying handle of the control is created (asynchronous)

The essence of Invoke is just a method, and the method must be called through the object.

When to use it?
Generally speaking, there are only two situations in which Invoke can be used:

Control的Invoke
Delegate的Invoke
In other words, Invoke is either preceded by a control or a delegate object.

Why use it?
1、Control的Invoke

Control's Invoke is generally used to solve the problem of cross-thread access. For example, if you want to operate a button button, you must use button.Invoke. If you want to operate a text label, you must use label.Invoke. But everyone will find it very troublesome , if I want to operate the button and the label, can I write them together? Is there an easier way to do this?

In fact, the main form is a Form, and Form naturally inherits Control, so Form also has the method of Invoke. If you want to save some trouble, you can directly call Form.Invoke, which is commonthis.Invoke.

Why is there nothing in front of some Invoke? In fact, this is in front of it, but it is omitted.

2、Delegate的Invoke

Delegate's Invoke actually calls the delegate method from the thread pool for execution. Invoke is a synchronous method that will block the UI thread that calls it. code show as below

public delegate void TestDelegateInvoke();

private void DelegateInvokeMethod()
{
    
    
        Thread.Sleep(5000);
}

private void btn_DelegateInvoke_Click(object sender , EventArgs e)
{
    
    
        TestDelegateInvoke testDelegate = new TestDelegateInvoke(DelegateInvokeMethod);

        testDelegate.Invoke();
}

After clicking the button to run, you will find that the UI interface will be stuck for 5 seconds.

Of course, it is not necessary to use the Invoke method to call the delegate, and it is also possible to directly call the delegate object. As follows:

public delegate void TestDelegateInvoke();

private void DelegateInvokeMethod()
{
    
    
     Thread.Sleep(5000);
}

private void btn_DelagateInvoke_Click(object sender, EventArgs e)
{
    
    
     TestDelegateInvoke testDelegate = new TestDelegateInvoke(DelegateInvokeMethod);

     testDelegate();
}

how to use?
1、Control 的 Invoke

For Control's Invoke, the more standard usage is to add judgment first, and then call

if(this.lbl_Value.InvokeRequired)
    {
    
    
        this.lbl_Value.Invoke(new Action(()=>
            {
    
    
    
                    this.lbl_Value.Text = "测试Invoke";
            }));
    }
    else
      {
    
    
                this.lbl_Value.Text = "测试Invoke";
    }

InvokeRequired is a property of Control, officially explained as:

Gets a value indicating whether the caller must call the Invoke method when making a method call on the control because the caller is in a thread other than the thread on which the control was created. true if the control's Handle was created on a different thread than the calling thread (indicating that you must call the control through the Invoke method); otherwise, false.

Simply put, if the control is operated by multiple threads, then this property is True, otherwise it is False.

2、Delegate的Invoke

In layman's terms, it is to call and execute the specified delegate on the main thread of an application. The main purpose is to let the working thread complete most of the computing work, and put the pure interface update in the UI thread to complete, so as to reduce the burden on the UI thread (avoid UI unresponsiveness).

//this.invoke的使用方法
//第一步:定义修改UI的方法
private void ModifyButton( bool _b )
{
    
    
   this.Button1.Enabled = _b;
}
//第二步:声明第一步方法的委托
private delegate void ModifyButton_dg( bool _b );
//第三步:调用委托
private void Calldelgate( )
{
    
    
   /*在Windows窗体应用程序中使用this.Invoke 在WPF应用程序中使用this.Dispatcher.Invoke*/
   this.Invoke( new ModifyButton_dg( ModifyButton ) ,new object[]{
    
    false});
}
//第四步:在非UI的线程中调用
  //创建线程 
      Thread _t = new Thread( new ThreadStart( threadmethod )); 
       _t.Start(); 
    //线程入口  
    private void threadmethod () 
     {
    
     
      Calldelgate(); 
     } 

Guess you like

Origin blog.csdn.net/m0_65636467/article/details/127804807
Recommended