CriticalSection.Enter vs TryEnter


I’ve been using CriticalSection.TryEnter all along since I can “control” the waiting time and even get out of a deadlock if it takes too long etc.

Now, for certain parts of my code a deadlock is not likely to happen (I actually designed it well, :slight_smile: ) and I’ve been using CriticalSection.Enter instead.

My question is: How does the CriticalSection.Enter really works? Is it more/less efficient? Just a hidden loop on the TryEnter method? If it’s a loop, for how long does it sleep the calling thread?

Thanks for any input!

Enter blocks the thread until it succeeds. If the resource never becomes available, it will block indefinitely.

Thanks Tim, that I know. I’m actually wondering about how it waits until it succeeds etc. i.e.: what are the inner works of “Enter”.

It’s probably OS dependent, but on Windows, it takes zero cpu, so it’s probably not a loop in the framework.