Use recommended async dispose pattern

Funny enough, non-breaking API changes since all of those classes are sealed.
This commit is contained in:
JustArchi
2022-06-19 18:24:52 +02:00
parent 82750352e2
commit 772607b680
7 changed files with 81 additions and 41 deletions

View File

@@ -29,7 +29,7 @@ using ArchiSteamFarm.Core;
namespace ArchiSteamFarm.Helpers;
internal sealed class CrossProcessFileBasedSemaphore : ICrossProcessSemaphore, IDisposable {
internal sealed class CrossProcessFileBasedSemaphore : IAsyncDisposable, ICrossProcessSemaphore, IDisposable {
private const ushort SpinLockDelay = 1000; // In milliseconds
private readonly string FilePath;
@@ -48,11 +48,23 @@ internal sealed class CrossProcessFileBasedSemaphore : ICrossProcessSemaphore, I
}
public void Dispose() {
// Those are objects that are always being created if constructor doesn't throw exception
LocalSemaphore.Dispose();
// Those are objects that might be null and the check should be in-place
FileLock?.Dispose();
}
public async ValueTask DisposeAsync() {
// Those are objects that are always being created if constructor doesn't throw exception
LocalSemaphore.Dispose();
// Those are objects that might be null and the check should be in-place
if (FileLock != null) {
await FileLock.DisposeAsync().ConfigureAwait(false);
}
}
void ICrossProcessSemaphore.Release() {
// ReSharper disable once SuspiciousLockOverSynchronizationPrimitive - this is not a mistake, we need extra synchronization, and we can re-use the semaphore object for that
lock (LocalSemaphore) {