<p>I have an Image control with it's source bound to a property on an object(string url to an image).  After making a service call, i update the data object with a new URL.  The exception is thrown after it leaves my code, after invoking the PropertyChanged event.</p>

<p>The data structure and the service logic are all done in a core dll that has no knowledge of the UI.  How do I sync up with the UI thread when i cant access a Dispatcher? </p>

<p>PS:  Accessing Application.Current.RootVisual in order to get at a Dispatcher is not a solution because the root visual is on a different thread(causing the exact exception i need to prevent).  </p>

<p>PPS: This only is a problem with the image control, binding to any other ui element,  the cross thread issue is handled for you.</p>

[{'answer_id': 47294, 'author': 'Jonathan Parker', 'author_id': 4504, 'author_profile': 'https://Stackoverflow.com/users/4504', 'pm_score': 1, 'selected': False, 'text': '<p>Have you tried implementing <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx" rel="nofollow noreferrer">INotifyPropertyChanged</a>?</p>\n'}, {'answer_id': 404706, 'author': 'Rob Fonseca-Ensor', 'author_id': 21433, 'author_profile': 'https://Stackoverflow.com/users/21433', 'pm_score': 0, 'selected': False, 'text': "<p>The property getter for RootVisual on the Application class has a thread check which causes that exception. I got around this by storing the root visual's dispatcher in my own property in my App.xaml.cs:</p>\n\n<pre><code>public static Dispatcher RootVisualDispatcher { get; set; }\n\nprivate void Application_Startup(object sender, StartupEventArgs e)\n{\n    this.RootVisual = new Page();\n    RootVisualDispatcher = RootVisual.Dispatcher;\n}\n</code></pre>\n\n<p>If you then call BeginInvoke on App.RootVisualDispatcher rather than Application.Current.RootVisual.Dispatcher you shouldn't get this exception.</p>\n"}, {'answer_id': 1261531, 'author': 'Brandon ', 'author_id': 151840, 'author_profile': 'https://Stackoverflow.com/users/151840', 'pm_score': 0, 'selected': False, 'text': "<p>I ran into a similar issue to this, but this was in windows forms:</p>\n\n<p>I have a class that has it's own thread, updating statistics about another process, there is a control in my UI that is databound to this object.  I was running into cross-thread call issues, here is how I resolved it:</p>\n\n<pre><code>Form m_MainWindow; //Reference to the main window of my application\nprotected virtual void OnPropertyChanged(string propertyName)\n{\n  if(PropertyChanged != null)\n    if(m_MainWindow.InvokeRequired)\n      m_MainWindow.Invoke(\n        PropertyChanged, this, new PropertyChangedEventArgs(propertyName);\n    else\n      PropertyChanged(this, new PropertyChangedEventArgs(propertyName);\n}\n</code></pre>\n\n<p>This seems to work great, if anyone has suggestions, please let me know.</p>\n"}, {'answer_id': 1929825, 'author': 'Phil', 'author_id': 138757, 'author_profile': 'https://Stackoverflow.com/users/138757', 'pm_score': 3, 'selected': False, 'text': '<pre><code>System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =&gt; {...});\n</code></pre>\n\n<p>Also look <a href="https://stackoverflow.com/questions/1924408/invalid-cross-thread-access-issue/1925827#1925827">here.</a></p>\n'}, {'answer_id': 16381177, 'author': 'Doitdotnet', 'author_id': 2348909, 'author_profile': 'https://Stackoverflow.com/users/2348909', 'pm_score': 0, 'selected': False, 'text': '<p>When ever we want to update UI related items that action should happen in the UI thread else you will get an invalid cross thread access exception</p>\n\n<pre><code>Deployment.Current.Dispatcher.BeginInvoke( () =&gt;\n\n{\n\nUpdateUI(); // DO the actions in the function Update UI\n\n});\n\npublic void UpdateUI()\n\n{\n\n//to do :Update UI elements here\n\n}\n</code></pre>\n'}, {'answer_id': 18246948, 'author': 'Jignesh.Raj', 'author_id': 1928157, 'author_profile': 'https://Stackoverflow.com/users/1928157', 'pm_score': 0, 'selected': False, 'text': '<p>The <code>INotifyPropertyChanged</code> interface is used to notify clients, typically binding clients, that a property value has changed.</p>\n\n<p>For example, consider a Person object with a property called FirstName. To provide generic property-change notification, the Person type implements the INotifyPropertyChanged interface and raises a PropertyChanged event when FirstName is changed.</p>\n\n<p>For change notification to occur in a binding between a bound client and a data source, your bound type should either:</p>\n\n<p>Implement the <code>INotifyPropertyChanged</code> interface (preferred).</p>\n\n<p>Provide a change event for each property of the bound type.</p>\n\n<p>Do not do both.</p>\n\n<p><strong>Example:</strong></p>\n\n<pre><code>using System;\nusing System.Collections.Generic;\nusing System.ComponentModel;\nusing System.Drawing;\nusing System.Runtime.CompilerServices;\nusing System.Windows.Forms;\n\n// Change the namespace to the project name. \nnamespace TestNotifyPropertyChangedCS\n{\n    // This form demonstrates using a BindingSource to bind \n    // a list to a DataGridView control. The list does not \n    // raise change notifications. However the DemoCustomer type  \n    // in the list does. \n    public partial class Form1 : Form\n    {\n        // This button causes the value of a list element to be changed. \n        private Button changeItemBtn = new Button();\n\n        // This DataGridView control displays the contents of the list. \n        private DataGridView customersDataGridView = new DataGridView();\n\n        // This BindingSource binds the list to the DataGridView control. \n        private BindingSource customersBindingSource = new BindingSource();\n\n        public Form1()\n        {\n            InitializeComponent();\n\n            // Set up the "Change Item" button.\n            this.changeItemBtn.Text = "Change Item";\n            this.changeItemBtn.Dock = DockStyle.Bottom;\n            this.changeItemBtn.Click +=\n                new EventHandler(changeItemBtn_Click);\n            this.Controls.Add(this.changeItemBtn);\n\n            // Set up the DataGridView.\n            customersDataGridView.Dock = DockStyle.Top;\n            this.Controls.Add(customersDataGridView);\n\n            this.Size = new Size(400, 200);\n        }\n\n        private void Form1_Load(object sender, EventArgs e)\n        {\n            // Create and populate the list of DemoCustomer objects \n            // which will supply data to the DataGridView.\n            BindingList&lt;DemoCustomer&gt; customerList = new BindingList&lt;DemoCustomer&gt;();\n            customerList.Add(DemoCustomer.CreateNewCustomer());\n            customerList.Add(DemoCustomer.CreateNewCustomer());\n            customerList.Add(DemoCustomer.CreateNewCustomer());\n\n            // Bind the list to the BindingSource. \n            this.customersBindingSource.DataSource = customerList;\n\n            // Attach the BindingSource to the DataGridView. \n            this.customersDataGridView.DataSource =\n                this.customersBindingSource;\n\n        }\n\n        // Change the value of the CompanyName property for the first  \n        // item in the list when the "Change Item" button is clicked.\n        void changeItemBtn_Click(object sender, EventArgs e)\n        {\n            // Get a reference to the list from the BindingSource.\n            BindingList&lt;DemoCustomer&gt; customerList =\n                this.customersBindingSource.DataSource as BindingList&lt;DemoCustomer&gt;;\n\n            // Change the value of the CompanyName property for the  \n            // first item in the list.\n            customerList[0].CustomerName = "Tailspin Toys";\n            customerList[0].PhoneNumber = "(708)555-0150";\n        }\n\n    }\n\n    // This is a simple customer class that  \n    // implements the IPropertyChange interface. \n    public class DemoCustomer : INotifyPropertyChanged\n    {\n        // These fields hold the values for the public properties. \n        private Guid idValue = Guid.NewGuid();\n        private string customerNameValue = String.Empty;\n        private string phoneNumberValue = String.Empty;\n\n        public event PropertyChangedEventHandler PropertyChanged;\n\n        // This method is called by the Set accessor of each property. \n        // The CallerMemberName attribute that is applied to the optional propertyName \n        // parameter causes the property name of the caller to be substituted as an argument. \n        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")\n        {\n            if (PropertyChanged != null)\n            {\n                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));\n            }\n        }\n\n        // The constructor is private to enforce the factory pattern. \n        private DemoCustomer()\n        {\n            customerNameValue = "Customer";\n            phoneNumberValue = "(312)555-0100";\n        }\n\n        // This is the public factory method. \n        public static DemoCustomer CreateNewCustomer()\n        {\n            return new DemoCustomer();\n        }\n\n        // This property represents an ID, suitable \n        // for use as a primary key in a database. \n        public Guid ID\n        {\n            get\n            {\n                return this.idValue;\n            }\n        }\n\n        public string CustomerName\n        {\n            get\n            {\n                return this.customerNameValue;\n            }\n\n            set\n            {\n                if (value != this.customerNameValue)\n                {\n                    this.customerNameValue = value;\n                    NotifyPropertyChanged();\n                }\n            }\n        }\n\n        public string PhoneNumber\n        {\n            get\n            {\n                return this.phoneNumberValue;\n            }\n\n            set\n            {\n                if (value != this.phoneNumberValue)\n                {\n                    this.phoneNumberValue = value;\n                    NotifyPropertyChanged();\n                }\n            }\n        }\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/46981', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/580/']