This project has moved and is read-only. For the latest updates, please go here.

Invalid cross-thread access.

Apr 24, 2011 at 4:44 PM

Hi :)

When I create an object, insert it in my database and add it to my ViewModel 

System.Diagnostics.Debug.WriteLine("key inserted => " + key.ToString());                                                
if (!App.ViewModel.Items.Contains(message))

I have this error when adding it to the viewModel (not when inserting in the db) :

System.UnauthorizedAccessException: Invalid cross-thread access.
� MS.Internal.XcpImports.CheckThread()
� System.Windows.DependencyObject.GetValueInternal(DependencyProperty dp)
� System.Windows.FrameworkElement.GetValueInternal(DependencyProperty dp)
� System.Windows.DependencyObject.GetValue(DependencyProperty dp)
� System.Windows.Controls.ItemsControl.get_ItemsHost()
� System.Windows.Controls.ItemsControl.OnItemsChangedHandler(Object sender, ItemsChangedEventArgs args)
� System.Windows.Controls.ItemContainerGenerator.OnItemAdded(Object item, Int32 index)
� System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.ICollectionChangedL

Do you know if I do something wrong? :)

Thank you

Apr 24, 2011 at 5:25 PM

Most likely the collection you are added to is bound to something in the UI, hence you are trying to update it from a non-UI thread. You should marshall the code that adds the item to the dispatcher.

Apr 24, 2011 at 8:23 PM

yep it works :D

Thanks a lot for you help.