Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

WPF Data binding and thread safety

$
0
0

When binding to a collection as an ItemsSource, do I have to worry about thread safety?

In other words, I have a listbox, defined in XAML.

In the code, I have

//somewhere in global variables

List<myClass> myListOfStuff;

In the XAML:

<ListBox Name="mylistbox">

<ListBox.ItemTemplate>

    <DataTemplate>

        <TextBlock Text={Binding Path=myProperty}>

    </ etc....... 

In the codebehind of my XAML

void somefunction()

{

mylistbox.ItemsSource=Globals.MyListOfStuff;

}

So, I open up my window with the list box in it, and at some point I trigger somefunction, which binds the itemsSource to a global list.  In the Item template for the list box, it has a text block bound to a property of myClass, so what I want is to see one line with that property for each item in the list.

I'm guessing that in order to display those values, it has to iterate through the list.

If that list is accessed by multiple threads, what happens if one of those other threads adds a new element to the list at about the same time that something on my XAML page is causing the somefunction to execute?  Is there any way to lock the object during that access?  Which piece of code would I lock, anyway?  Or does some sort of lock happen by magic?


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>