Home > Net >  conditionVariable.wait_for() equivalent in C#
conditionVariable.wait_for() equivalent in C#

Time:12-03

In C , we can use the function

wait_for(lock, delay, []{return i == 1;})) 

with condition variables for synchronization, I start working in a small application using C# (my first contact with C#), and I need the exact same functionality, but I did not find a good substitue to this function.

I there any function that did the exact same behaviour or I need to implement the logic myself ?

CodePudding user response:

For asynchronous code I would suggest using Semaphore Slim. It's very fast and easy to use.
As for sync code, easiest way to do such thing would be to use lock statement as Matthew suggested.

CodePudding user response:

You wont find exactly the same one-liner for your code. However you might be interested in using the code example

public static async Task WaitUntil(Func<bool> condition, int frequency = 25, int timeout = -1)

from the stackoverflow thread C# Wait until condition is true, which implements the described functionality for C# tasks (not threads).

  • Related