diff --git a/Wino.Core/Integration/WinoImapClient.cs b/Wino.Core/Integration/WinoImapClient.cs index 5701cf93..f4712268 100644 --- a/Wino.Core/Integration/WinoImapClient.cs +++ b/Wino.Core/Integration/WinoImapClient.cs @@ -1,4 +1,6 @@ -using MailKit; +using System; +using System.Threading; +using MailKit; using MailKit.Net.Imap; using Serilog; @@ -9,6 +11,8 @@ namespace Wino.Core.Integration; /// internal class WinoImapClient : ImapClient { + private int _busyCount; + /// /// Gets or internally sets whether the QRESYNC extension is enabled. /// It is set by ImapClientPool immidiately after the authentication. @@ -47,6 +51,34 @@ internal class WinoImapClient : ImapClient } } + public bool IsBusy() => _busyCount > 0; + + public IDisposable GetBusyScope() + { + Interlocked.Increment(ref _busyCount); + return new BusyScope(this); + } + + private class BusyScope : IDisposable + { + private readonly WinoImapClient _client; + private bool _disposed; + + public BusyScope(WinoImapClient client) + { + _client = client; + } + + public void Dispose() + { + if (!_disposed) + { + Interlocked.Decrement(ref _client._busyCount); + _disposed = true; + } + } + } + protected override void Dispose(bool disposing) { base.Dispose(disposing);