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 7820801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:26:04+00:00 2026-06-02T07:26:04+00:00

I just want to change the window’s background in another thread. there are two

  • 0

I just want to change the window’s background in another thread. there are two program, one is work right, and the other throw an InvalidOperationException.

The right code:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Thread t = new Thread(new ParameterizedThreadStart(threadTest));
        t.Start(@"C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg");
    }

    void threadTest(object obj)
    {
        string path = obj as string;
        this.Dispatcher.Invoke(new Func<object>(() => this.Background = new 
    }
}

the Error Code:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Thread t = new Thread(new ParameterizedThreadStart(threadTest));
        t.Start(@"C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg");
    }

    void threadTest(object obj)
    {
        string path = obj as string;
        //this.Dispatcher.Invoke(new Func<object>(() => this.Background = new ImageBrush(new BitmapImage(new Uri(path)))));
        ImageBrush background = new ImageBrush(new BitmapImage(new Uri(path)));
        this.Dispatcher.Invoke(new Func<object>(() => this.Background = background));
    }
}

the different between these codes is that, the error code create the ImageBrush object in the child thread.
So my question is that: in the wpf program, is the thread can only use the objects creates by own thread?
thanks for any reply.

  • 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-02T07:26:05+00:00Added an answer on June 2, 2026 at 7:26 am

    Yes, you are right. Only the UI thread can use objects created by it. So, you can use the Dispatcher to “enqueue” the UI operations on it’s proper thread.

    Answering your second question, sure, there’s a way to “pass” objects to the UI Thread. If you see the BeginInvoke structure (of the Dispatcher) it’s:

    public DispatcherOperation BeginInvoke(
        Delegate d,
        params Object[] args
    )
    

    Where the args is the params object array, there’s where you put the params.

    Now, if you are using some Freezable object (for example some Image, Brush, Transform or Geometry) then you need to object.Freeze(); before send it to the UI Thread.

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

Sidebar

Related Questions

Just want to know if there other optional wrapper framework available that generate client
Can't be this hard can it!? I just want to change window.location onclientclick of
Say you just want to get rid of the changes you've made to one
I just want to make sure I'm doing the right thing. I have a
I want to move my form window by left-clicking and holding on the background
Is there any way to change the color of an empty SDL window to
In my wpf window I want to change part of it (make different controls
I want to workaround the Webpage has expired issue. First, I just changed a
Just want to make sure I understand this correctly (I'd ask on SO Chat,
Just want to know how to read an attribute of a parent node from

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.