From the course: Advanced C#: Thread-Safe Data with Concurrent Collections
Unlock this course with a free trial
Join today to access over 22,500 courses taught by industry experts.
Use a while loop with TryUpdate - C# Tutorial
From the course: Advanced C#: Thread-Safe Data with Concurrent Collections
Use a while loop with TryUpdate
- [Presenter] Now we know that when you call TryUpdate it won't modify the value in the dictionary entry if another thread has changed the value. That's great, but what do we do with that information? Well, obviously that depends on what kind of application you're building and what you need to do with your code, but I see two main paths. I can either abandon my attempt to update the value, or I can retry later and see if I get another opportunity to update the value after the interfering thread is done, and that's the approach we'll look at in this demo. Let's start here on line 89. Remember that TryUpdate returns false if it can't update the value. So that's what I'm checking here. I'm saying run the code in this loop only if you get back false. So let's look at the first scenario. We're going to run the code, and no other thread has interfered with the value. So you come down here to line 89, this evaluates to true so it…
Contents
-
-
-
-
-
-
-
-
(Locked)
Review the updated example application1m 34s
-
(Locked)
Potential problems with updating an item3m
-
(Locked)
Use TryUpdate to update dictionary value2m 4s
-
(Locked)
Use a while loop with TryUpdate2m 5s
-
(Locked)
Use the AddOrUpdate method4m 16s
-
(Locked)
Why ICollection and other interfaces are not thread-safe2m 32s
-
(Locked)
Inspect your APIs for ICollection usage2m 40s
-
(Locked)
-
-
-