Home > OS >  Is there a way to know C# semaphore slim wait time
Is there a way to know C# semaphore slim wait time

Time:01-24

I am using SemaphoreSlim. I want to know how long the .Wait() was spending before it could get the semaphore. I know there is an overload that can ask to wait for x milliseconds and then quit. But that does not tell me how much time exactly it would take. I am willing to wait but want to know how much time the wait was spent.

CodePudding user response:

If after the Wait returns you want to know how much the Wait taken - then just use StopWatch

If you want to know this before calling Wait or during such a call, then there is no built-in mechanism for this. And I believe such a generic mechanism cannot be implemented as the developer itself controls synchronization on its own. So, depending on your sinchinization and computation logic, you can estimate the time remainig before other part of your code will free the semaphore.In some cases, when you are working with some external (and even - remote) services, it may be hard or even imposible to make a good estimate.

  •  Tags:  
  • Related