From the course: Advanced Threading in C#
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Thread affinity
- [Instructor] So what is thread affinity? If you've worked on a WPF or Winforms application, any attempt to access the UI element from any other thread may lead to an exception or could even render the state of the control invalid. So thread affinity means that the thread, in this case the UI thread that instantiates an object is the only thread that can access its members. So for example, dependency object in WPF has thread affinity. So is thread affinity good or bad? The answer is, it depends. One of the advantages of having thread affinity is that we may not need to use a lock to access a UI object in WPF. For that matter, if any thread has thread affinity we should be able to access the objects inside that thread simply because no other thread will be able to access those objects, so we don't really need to have a lock around them. However, it could also be disadvantageous in certain situations. So for example, if I want to make a call to a member on object X created by thread X…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.