Must create DependencySource on same Thread as DependencyObject

為{幸葍}努か 提交于 2019-11-30 03:47:04

问题


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.


回答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.




回答2:


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.




回答3:


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;


来源:https://stackoverflow.com/questions/7865514/must-create-dependencysource-on-same-thread-as-dependencyobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!