编程语言
首页 > 编程语言> > C# 异步锁

C# 异步锁

作者:互联网

参考网址: https://www.cnblogs.com/Alicia-meng/p/13330640.html

private static readonly SemaphoreSlim _mutex = new SemaphoreSlim(1);

        static int _value;

        public async static Task DelayAndIncrementAsync()
        {
            await _mutex.WaitAsync();
            try
            {
                var oldValue = _value;
                await Task.Delay(TimeSpan.FromSeconds(oldValue));
                Console.WriteLine(_value);
                _value = oldValue + 1;

            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.Message);
            }
            finally
            {

                _mutex.Release();
            }
        }

标签:异步,SemaphoreSlim,C#,value,信号量,static,Semaphore,mutex
来源: https://www.cnblogs.com/bruce1992/p/15128173.html