c# - WPF Event: BitmapImage PropertyChanged: "Calling Thread Cannot access" -
I am trying to understand what the following code is about which is loading < / B> Displaying a text file and its contents, but is not happy with loading the betamap image and appearing on the timer. Alleased Event Handler
I understand that it has to do with the UI Thread.
But why is this not a problem for textfile example?
First of all, XAML:
& lt; Window x: orbit = "wpfApplication7 .indow1" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/winfx/ 2006 / xaml "title =" window 1 "height =" 300 "width =" 300 "& gt; & Lt; StackPanel Orientation = "Vertical" & gt; & Lt; TextBlock Text = "{Binding Path = Message, Update Instructor = Property Changes}" fontSize = "20" height = "40" width = "300" background = "ALSWB" /> & Lt; Image source = "{binding path = image, update sourcerame = property changed]" height = "100" width = "100" /> & Lt; / StackPanel & gt; & Lt; / Window & gt; and C #, which raises an estate changelog handler on a timer:
using the system; Using System.ComponentModel; Using System.Timers; Using System.Windows; Using System.IO; Using System.Windows.Threading; System.Windows.Media Using imaging; and
namespace WpfApplication7 {public partial squares window 1: window, INotifyPropertyChanged {public BitmapImage Image {get; Private set; } Public string message {get; Set; } Public Event PropertyChangedEventHandler PropertyChanged = delegate {}; Private Timer Timer; Public Window 1 () {InitializeComponent (); this. DataContext = This; This.timer = New timer {enabled = true, interval = 100}; This.timer.Elapsed + = (S, E) = & gt; {// --- Loading happy with text file UI updates :) this.Message = File.ReadAllText (@ "c: \ windows \ win.ini") Substrings (0, 20); Property changed (this, new asset changed event event ("message")); // --- No bitmap imaging loading. The property is unhappy :( // (I do not have to !! !!) // Application.Dispatcher.InvoC (// Dispatcher advance, assigned new action (rep // // this.Image = new bitmapImage (new Uri (@ "C: \ windows web \ wallpaper \ upcent.jpg"); // Edit - EhHah, Thanks Daniel! // Dependency Object-> FreeJoyel-> Animable-> ImageSource-> Bitmap Source - & Gt; Bitmap import this image, image.fridge (); I know I can fix it with an "application. Current dispatcher. Invoke". Therefore fixing is not a problem. I do not understand why I have a problem :)
same question
I think important between two scenarios The difference is that the bitmap image is a dependency object, which means that it contains the "owner" thread (the thread created for the object). When your main UI thread tries to use the built-in bitmapmage object (and proprietary) on another thread ... Boom!
Strings, on the other hand, are not a concept of "owner" threads.
Comments
Post a Comment