When to use invoke and begininvoke in wpf
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Sachin SinghRank #66 · 26.2K reputationPosted Jun 17, 2021, 5:37 AM EDT
Control.Invoke: Executes on the UI thread, but calling thread waits for completion before continuing.Control.BeginInvoke: Executes on the UI thread, and calling thread doesn't wait for completion.Delegate.Invoke: Executes synchronously, on the same thread.Delegate.BeginInvoke: Executes asynchronously, on athreadpoolthread.credit:- https://stackoverflow.com/questions/229554/whats-the-difference-between-invoke-and-begininvoke
Rajanikant HawaldarRank #108 · 20.5K reputationPosted Jun 17, 2021, 5:45 AM EDT
Invoke is Synchronous whereas BeginInvoke is Asynchronous, which means in case of Invoke, control will not
return to the calling object until the callback returns. If you are OK with blocking calls, then you can choose Invoke.
create one simple app add
one textbox and enter some text and add two buttons and see how threads are working.
public MainWindow() { InitializeComponent(); txt.Text = "HI"; } private void invoke_Click(object sender, RoutedEventArgs e) { string enteredText = ""; txt.Dispatcher.Invoke(new Action(() => enteredText = txt.Text), null); MessageBox.Show(enteredText); } private void begininvoke_Click(object sender, RoutedEventArgs e) { string enteredText = ""; txt.Dispatcher.BeginInvoke(new Action(() => enteredText = txt.Text), null); MessageBox.Show(enteredText); }Patel73 reputationPosted Jun 17, 2021, 5:42 AM EDT