What's the difference between Invoke() and BeginInvoke()

前端 未结 6 1830
无人共我
无人共我 2020-11-22 01:14

Just wondering what the difference between BeginInvoke() and Invoke() are?

Mainly what each one would be used for.

EDIT: What is t

6条回答
  •  爱一瞬间的悲伤
    2020-11-22 02:12

    Just to give a short, working example to see an effect of their difference

    new Thread(foo).Start();
    
    private void foo()
    {
      this.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
        (ThreadStart)delegate()
        {
            myTextBox.Text = "bing";
            Thread.Sleep(TimeSpan.FromSeconds(3));
        });
      MessageBox.Show("done");
    }
    

    If use BeginInvoke, MessageBox pops simultaneous to the text update. If use Invoke, MessageBox pops after the 3 second sleep. Hence, showing the effect of an asynchronous (BeginInvoke) and a synchronous (Invoke) call.

提交回复
热议问题