I have single resource (StorageFolder) and which I access in operations:
- AddFileAsync
- ReadFileAsync
- DeleteFolderAsync
I use SemaphoreSlim.WaitAsync
class to lock the resource, so it won't be accessed by multiple operations at the same time.
What .net class should I use instead of SemaphoreSlim
so that I will be able to call multiple AddFileAsync
. and ReadFileAsync
concurently, but I won't be able to call AddFileAsync
(resp ReadFolderAsync
) and DeleteFolderAsync
concurently.
In other words, I want to prevent deleting folder while other thread is reading/wrinting to it.
current code (try/finally blocks ommited):
_sempaphore = new SemaphoreSlim(1);
async Task AddFileAsync(){
await _sempaphore.WaitAsync();
await _storageFolder.CreateFileAsync(....)
_sempaphore.Release();
}
async Task DeleteFolderAsync(){
await _sempaphore.WaitAsync();
await _storageFolder.DeleteAsync()
_sempaphore.Release();
}