Skip to content

How to use AsyncNonKeyedLocker

Mark Cilia Vincenti edited this page Jan 16, 2024 · 7 revisions

Using AsyncNonKeyedLocker is straightforward. Usually you'd create an instance of AsyncNonKeyedLocker as a class member. If your class is not a singleton and you need to lock even against other instances of your class, then make sure you set it as static.

Example usage:

using AsyncKeyedLock;

namespace MyNamespace
{
   class MyClass
   {
      private static AsyncNonKeyedLocker _myLock = new AsyncNonKeyedLocker(maxCount: 1);

      public async Task DoSomething(CancellationToken cancellationToken)
      {
         using (_myLock.LockAsync(cancellationToken))
         {
            ...
         }
      }
   }
}