前言:
不要在async和await中使用lock,也不要在lock中使用await。
- 可以替换为 SemaphoreSlim,示例代码如下:
calss Test{
private static readonly SemaphoreSlim _mutex = new SemaphoreSlim(1, 1);
public async Task InsertCourse()
{
try
{
await _mutex.WaitAsync();
//...
}catch(Exception ex)
//...
}finally
{
_mutex.Release();
}
}
//...
}