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

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