Invoke(Delegate)

后端 未结 9 1047
误落风尘
误落风尘 2020-11-22 16:33

Can anybody please explain this statement written on this link

Invoke(Delegate):

Executes the specified delegate on the thread that owns th

相关标签:
9条回答
  • 2020-11-22 16:43

    In practical terms it means that the delegate is guaranteed to be invoked on the main thread. This is important because in the case of windows controls if you don't update their properties on the main thread then you either don't see the change, or the control raises an exception.

    The pattern is:

    void OnEvent(object sender, EventArgs e)
    {
       if (this.InvokeRequired)
       {
           this.Invoke(() => this.OnEvent(sender, e);
           return;
       }
    
       // do stuff (now you know you are on the main thread)
    }
    
    0 讨论(0)
  • 2020-11-22 16:43

    It means that the delegate you pass is executed on the thread that created the Control object (which is the UI thread).

    You need to call this method when your application is multi-threaded and you want do some UI operation from a thread other than the UI thread, because if you just try to call a method on a Control from a different thread you'll get a System.InvalidOperationException.

    0 讨论(0)
  • 2020-11-22 16:44

    If you want to modify a control it must be done in the thread in which the control was created. This Invoke method allows you to execute methods in the associated thread (the thread that owns the control's underlying window handle).

    In below sample thread1 throws an exception because SetText1 is trying to modify textBox1.Text from another thread. But in thread2, Action in SetText2 is executed in the thread in which the TextBox was created

    private void btn_Click(object sender, EvenetArgs e)
    {
        var thread1 = new Thread(SetText1);
        var thread2 = new Thread(SetText2);
        thread1.Start();
        thread2.Start();
    }
    
    private void SetText1() 
    {
        textBox1.Text = "Test";
    }
    
    private void SetText2() 
    {
        textBox1.Invoke(new Action(() => textBox1.Text = "Test"));
    }
    
    0 讨论(0)
  • 2020-11-22 16:52

    Delegate are essentially inline Action's or Func<T>. You can declare a delegate outside the scope of a method which you are running or using a lambda expression(=>); because you run the delegate within a method, you run it on the thread which is being run for the current window/application which is the bit in bold.

    Lambda example

    int AddFiveToNumber(int number)
    {
      var d = (int i => i + 5);
      d.Invoke(number);
    }
    
    0 讨论(0)
  • 2020-11-22 16:55

    It means that the delegate will run on the UI thread, even if you call that method from a background worker or thread-pool thread. UI elements have thread affinity - they only like talking directly to one thread: the UI thread. The UI thread is defined as the thread that created the control instance, and is therefore associated with the window handle. But all of that is an implementation detail.

    The key point is: you would call this method from a worker thread so that you can access the UI (to change the value in a label, etc) - since you are not allowed to do that from any other thread than the UI thread.

    0 讨论(0)
  • 2020-11-22 17:01

    A control or window object in Windows Forms is just a wrapper around a Win32 window identified by a handle (sometimes called HWND). Most things you do with the control will eventually result in a Win32 API call that uses this handle. The handle is owned by the thread that created it (typically the main thread), and shouldn't be manipulated by another thread. If for some reason you need to do something with the control from another thread, you can use Invoke to ask the main thread to do it on your behalf.

    For instance, if you want to change the text of a label from a worker thread, you can do something like this:

    theLabel.Invoke(new Action(() => theLabel.Text = "hello world from worker thread!"));
    
    0 讨论(0)
提交回复
热议问题