Must create DependencySource on same Thread as DependencyObject

12,699

Solution 1

The SolidColorBrush is a Freezable which is a derived DispatcherObject. DispatcherObjects have thread affinity - i.e it can only be used/interacted with on the thread on which it was created. Freezables however do offer the ability to freeze an instance. This will prevent any further changes to the object but it will also release the thread affinity. So you can either change it so that your property is not storing a DependencyObject like SolidColorBrush and instead just store the Color. Or you can freeze the SolidColorBrush that you are creating using the Freeze method.

Solution 2

Its not enough to set your dataGrid.ItemsSource on the main thread. You must create each item on the main-thread. Something like:

List<SomeObject> l = new List<SomeObject>();
foreach(var item in ListOfItemsInstance.ListToBind)
{
    l.Add(new SomeObject(){NameOfItem = item.NameOfItem, Properties = item.Properties });
}

dataGrid.ItemsSource = l;

Solution 3

I think the standard way is to derive the data object from Freezable and Freeze it before passing it to another thread. Once the object is frozen, you can't change it any more, so there's no danger of threading bugs.

Another option might be to make the data object a plain C# object (not derived from DispatcherObject) and implement INotifyPropertyChanged yourself.

Share:
12,699
Ondrej Janacek
Author by

Ondrej Janacek

I'm a C# developer at Logis Solutions and a former Microsoft MVP for .NET (2015-2017). LinkedIn

Updated on June 05, 2022

Comments

  • Ondrej Janacek
    Ondrej Janacek about 2 years

    I have an application written in wpf, which downloads some webpages, parses html code and saves some values.

    class ListOfItems
    {    
        public List<SomeObject> ListToBind;
        public void DownloadItems()
        { 
            Task.Factory.StartNew(() => 
            {
                ...
                ...
                if (OnDownloadCompleted != null)
                    OnDownloadCompleted(this, EventArgs.Empty);
            }
        }
    }
    
    class SomeObject
    {
        public string NameOfItem;
        public MyClass Properties;
    }
    
    class MyClass
    {
        public int Percentage;
        public SolidColorBrush Color;
    }
    

    This is the object model I'm using. It's simplified version and I don't want you to reorganize it, there is a reason I wrote it this way. In ListOfItems class is method which does all the job (there are some other methods used inside to make code readable) - downloads source, parses and fills ListToBind with data, f.e.

    [0] => NameOfItem = "FirstOne", Properties = {99, #FF00FF00}
    [1] => NameOfItem = "SecondOne", Properties = {50, #FFFF0000}
    etc.
    

    As you can see, when this method DownloadItems completes its job, OnDownloadCompleted event is raised. In the main thread is following code

    void listOfItems_OnDownloadCompleted(object sender, EventArgs args)
    {
        dataGrid.Dispatcher.Invoke(new Action(() => { 
                    dataGrid.ItemsSource = ListOfItemsInstance.ListToBind;
                }));
    }
    

    DataGrid on the MainWindow.xaml is filled with values, because of following xaml code snippet.

    <DataGrid Name="dataGrid" AutoGenerateColumns="False">
        <DataGrid.Columns>
             <DataGridTextColumn Header="Tag" Binding="{Binding Name}"/>
             <DataGridTextColumn Header="Color" Binding="{Binding MyClass.Percentage}">
                 <!--<DataGridTextColumn.CellStyle>
                     <Style TargetType="DataGridCell">
                         <Setter Property="Background" Value="{Binding MyClass.Color}" />
                     </Style>
                 </DataGridTextColumn.CellStyle>-->
             </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>
    

    It works just fine. But there is this problem. Try to uncomment commented xaml snippet and you will get Must create DependencySource on same Thread as the DependencyObject. error.

    Finally, my question is, how to avoid this error?

    EDIT:

    It should look like this in the end. This picture is taken from MS Excel and coloured in Adobe Photoshop.

    example

  • Ondrej Janacek
    Ondrej Janacek over 12 years
    Thank you for this explanation. The second answer in to this question is probably ment the same way, but problem was I didn't know, what to Freeze. Now, when you told me SolidColorBrush is derived from DispatcherObject and explained the situation, I made it work. Thank you again, bounty is your.
  • user1912383
    user1912383 over 8 years
    PropertyChanged events don't necessarily have to take place on ui thread, because they are [automatically marshaled to the UI thread. [stackoverflow.com/a/11015784]
  • Maxime Tremblay-Savard
    Maxime Tremblay-Savard almost 5 years
    This is the only detail I was missing! The task should only fetch the data you need and then you build your observable collection with it.