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

  • Home
  • SEARCH
  • 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 3403608
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:16:54+00:00 2026-05-18T05:16:54+00:00

I realize what I’m doing is probably pretty silly, but I’m in the middle

  • 0

I realize what I’m doing is probably pretty silly, but I’m in the middle of learning WPF and would like to know how to do this.

I have a window with a listbox on it. The listbox is being used to deliver status messages about the program while it’s running. For example “Server started” “New connection at IP #” etc. I wanted this to be constantly updating in the background, so I spawned a new thread for handling updating this, but when I made the call to add an item I get the error message “The calling thread cannot access this object because a different thread owns it.”

Any idea how I can update the listbox from another thread? Or in the background, etc.

  • 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-05-18T05:16:55+00:00Added an answer on May 18, 2026 at 5:16 am

    UPDATE

    If you are using C# 5 and .NET 4.5 or above you can avoid getting on another thread in the first place using async and await, e.g.:

    private async Task<string> SimLongRunningProcessAsync()
    {
        await Task.Delay(2000);
        return "Success";
    }
    
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        button.Content = "Running...";
        var result = await SimLongRunningProcessAsync();
        button.Content = result;
    }
    

    Easy:

    Dispatcher.BeginInvoke(new Action(delegate() 
      {
         myListBox.Items.Add("new item"));
      }));
    

    If you are in code-behind. Otherwise you can access the Dispatcher (which is on every UIElement) using:

    Application.Current.MainWindow.Dispatcher.BeginInvoke(...
    

    Ok thats a lot in one line let me go over it:

    When you want to update a UI control you, as the message says, have to do it from the UI thread. There is built in way to pass a delegate (a method) to the UI thread: the Dispatcher. Once you have the Dispatcher you can either Invoke() of BeginInvoke() passing a delegate to be run on the UI thread. The only difference is Invoke() will only return once the delegate has been run (i.e. in your case the ListBox’s new item has been added) whereas BeginInvoke() will return immediately so your other thread you are calling from can continue (the Dispatcher will run your delegate soon as it can which will probably be straight away anyway).

    I passed an anonymous delegate above:

    delegate() {myListBox.Items.Add("new item");}
    

    The bit between the {} is the method block. This is called anonymous because only one is created and it doesnt have a name (usually you can do this using a lambda expression but in this case C# cannot resolve the BeginInvoke() method to call). Or I could have instantiated a delegate:

    Action myDelegate = new Action(UpdateListMethod);
    
    void UpdateListMethod() 
    {
      myListBox.Items.Add("new item");
    }
    

    Then passed that:

    Dispatcher.Invoke(myDelegate);
    

    I also used the Action class which is a built in delegate but you could have created your own – you can read up more about delegates on MSDN as this is going a bit off topic..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realize this would violate convention, but I'm curious to know if you can
I realize this is fairly abstract, but I'd like to do something not unlike
I realize this question (or questions that look like this) have been asked over
I realize this may be a very simple question but I need to know
I realize that this can be a matter of preference, but I have noticed
I realize that this would be COMPLETELY bad practice in normal situations, but this
I realize this has been asked countless times, but I have yet to come
I realize this is likely an inane question, but here goes: I'd like to
I realize this seems like an obvious JS type thing - but I'm new
I realize this is syntactically bad but I figure it somewhat explains what I'm

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.