Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7723757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:31:19+00:00 2026-06-01T04:31:19+00:00

I have the doubt regarding when to use the Dispatcher.Invoke to update something on

  • 0

I have the doubt regarding when to use the Dispatcher.Invoke to update something on UI from different Thread.

Here’s my code…

public Window4()
    {
        InitializeComponent();
        this.DataContext = this;

      Task.Factory.StartNew(() => Test() );
    }

    private List<string> listOfString = new List<string>();

    public List<string> ListOfString
    {
        get { return listOfString; }
        set { listOfString = value; }
    }

    public void Test()
    {
        listOfString.Add("abc");
        listOfString.Add("abc");
        listOfString.Add("abc");
    }

 <Grid>
    <ListView ItemsSource="{Binding ListOfString}" />
</Grid>

I am starting a new Task on the different Thread, do i need to use Dispatcher.BeginInvoke to update the UI.

In this case it is updating the UI, but i’ve seen some scenarios where people update UI using Dispatcher.Invoke or BeginInvoke from the different Thread.

So my question is when we have to do that and why in this case it is working fine.

Thanks & Regards,
BHavik

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:31:21+00:00Added an answer on June 1, 2026 at 4:31 am

    I have the doubt regarding when to use the Dispatcher.Invoke to update
    something on UI from different Thread.

    When you are on a different thread you will always have to use the dispatcher to update a ui component that belongs to another thread.

    I am starting a new Task on the different Thread, do i need to use
    Dispatcher.BeginInvoke to update the UI.

    Tasks allow for multiple operations to be performed without blocking the thread they are called from but that doesn’t mean they are on a different thread. However when updating the UI from inside a Task you will need to use the dispatcher.

    In this case it is updating the UI, but i’ve seen some scenarios where
    people update UI using Dispatcher.Invoke or BeginInvoke from the
    different Thread.

    Invoke will block the calling thread while it is performing the action and BeginInvoke will not. BeginInvoke will return control immediately to the caller, Invoke may cause the calling thread to hang if it is performing a heavy operation.

    This is from msdn documentation,

    In WPF, only the thread that created a DispatcherObject may access
    that object. For example, a background thread that is spun off from
    the main UI thread cannot update the contents of a Button that was
    created on the UI thread. In order for the background thread to access
    the Content property of the Button, the background thread must
    delegate the work to the Dispatcher associated with the UI thread.
    This is accomplished by using either Invoke or BeginInvoke. Invoke is
    synchronous and BeginInvoke is asynchronous.

    Edit: In response to your comment I ran some tests.

    When calling Test() from a task (without using the dispatcher) I got this error “The calling thread cannot access this object because a different thread owns it.”

    So I created a method called PrintThreadID(). I printed the thread before entering the task then from inside the task and it does report both are running on the same thread ID.

    The error is misleading because it says the calling thread is different than the one that owns it which the PrintThreadID() function shows is not true, they are in fact on the same thread. Tasks while on the same thread still cannot update a UI component without using Dispather.Invoke().

    So here is a working example which will update the Grid from a task.


    public partial class MainWindow : Window
    {
        public List<string> myList { get; private set; }
    
        public MainWindow()
        {
            InitializeComponent();
            myList = new List<string>();
            label1.Content = Thread.CurrentThread.ManagedThreadId.ToString();
    
            Task.Factory.StartNew(PrintThreadID);
            Task.Factory.StartNew(Test);
    
        }
    
        private void PrintThreadID()
        {
            label1.Dispatcher.Invoke(new Action(() =>
                label1.Content += "..." + Thread.CurrentThread.ManagedThreadId.ToString()));
        }
    
        private void Test()
        {
            myList.Add("abc");
            myList.Add("abc");
            myList.Add("abc");
    
            // if you do not use the dispatcher you will get the error "The calling thread cannot access this object because a different thread owns it."
    
    
            dataGrid1.Dispatcher.Invoke(new Action(() =>
            {
                dataGrid1.ItemsSource = myList.Select(i => new { Item = i });
            }));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one doubt regarding casting. public void Test(out T a, out T b)
I have a doubt regarding the backlog value in listen system call. From man
I have a doubt regarding downloading data from a web service. One way is
I have a doubt regarding RMI. In RMI we create remote object(s) and use
I have a doubt regarding POJO. Take below example public class User { String
I have a doubt regarding the use of servlets. In the application I'm building
I am new to stackoverflow i have doubt regarding calling jsp from javascript file.
I have a doubt regarding unions. Suppose there is a union defined as union
I have a doubt regarding how to get the pkcs11 driver for interacting with
I have a very basic doubt regarding the method that gets executed when app

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.