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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:30:21+00:00 2026-05-11T16:30:21+00:00

I’m trying to understand what it is about the following code that is perfectly

  • 0

I’m trying to understand what it is about the following code that is perfectly happy with loading a text file and displaying its contents, but isn’t happy with loading a BitmapImage and displaying it on a timer.Elapsed event handler.

I understand it has to do with the UI thread.

But why is this not a problem for the textfile example?

First, the XAML:

<Window x:Class="WpfApplication7.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">

<StackPanel Orientation="Vertical">

<TextBlock Text="{Binding Path=Message, UpdateSourceTrigger=PropertyChanged}" FontSize="20" Height="40" Width="300"  Background="AliceBlue"  />

<Image Source="{Binding Path=Image,UpdateSourceTrigger=PropertyChanged}" Height="100" Width="100"/>

</StackPanel>
</Window>

and the C#, which raises a PropertyChangedEventHandler on a timer:

using System;
using System.ComponentModel;
using System.Timers;
using System.Windows;
using System.IO;
using System.Windows.Threading;
using System.Windows.Media.Imaging;

and

namespace WpfApplication7
{
  public partial class Window1 : Window, INotifyPropertyChanged
  {
    public BitmapImage Image { get; private set; }
    public string Message { get; set; }
    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    private Timer timer;

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

      this.timer = new Timer { Enabled = true, Interval = 100 };

      this.timer.Elapsed += (s, e) =>
      {
        //---happy loading from text file. UI updates :)
        this.Message = File.ReadAllText(@"c:\windows\win.ini").Substring(0, 20);
        PropertyChanged(this, new PropertyChangedEventArgs("Message"));

        //---not happy loading a BitmapImage. PropertyChanged unhappy :(
        // (Don't make me have to: ! )
        //Application.Current.Dispatcher.Invoke(
        //DispatcherPriority.Send, new Action(delegate
        //{

          this.Image = new BitmapImage(new Uri(@"C:\WINDOWS\Web\Wallpaper\Ascent.jpg"));

          //Edit --Ah hah, thanks Daniel ! 
          // DependencyObject-> Freezable-> Animatable-> 
          // ImageSource-> BitmapSource-> BitmapImage
          this.Image.Freeze();  //<--- this will fix it, no need for Dispatcher

          //Without Dispatcher or Freeze() ... right here:
          //"The calling thread cannot access this object because a different thread owns it."
          PropertyChanged(this, new PropertyChangedEventArgs("Image"));
        //}));
      };
    }
  }
}

I know I can fix this with a “Application.Current.Dispatcher.Invoke”. So fixing it isn’t the problem. Not understanding why I should have to is the problem 🙂

Similar questions

  • asynchronously loading a BitmapImage in C# using WPF

  • Making sure OnPropertyChanged() is called on UI thread in MVVM WPF app

  • 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-11T16:30:22+00:00Added an answer on May 11, 2026 at 4:30 pm

    I think the critical difference between the two scenarios is that BitmapImage is a dependency object, which means it has the concept of an “owning” thread (the thread that created the object). When your main UI thread tries to access the BitmapImage object created on (and owned by) another thread…boom!

    Strings, on the other hand, do not have a concept of an “owning” thread.

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

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The biggest problem you have looks to be due to… May 12, 2026 at 12:56 am
  • Editorial Team
    Editorial Team added an answer You can use the wordwrap() function, then explode on newline… May 12, 2026 at 12:55 am
  • Editorial Team
    Editorial Team added an answer Just in case it's still relevant, and for future users:… May 12, 2026 at 12:55 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.