1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-07 16:06:15 +00:00

Dispose handling order book

This commit is contained in:
Jkorf 2022-02-22 12:54:19 +01:00
parent c22b54c898
commit 5c99da6617
3 changed files with 44 additions and 3 deletions

View File

@ -152,6 +152,9 @@ namespace CryptoExchange.Net
/// <returns></returns> /// <returns></returns>
protected virtual async Task<CallResult<UpdateSubscription>> SubscribeAsync<T>(SocketApiClient apiClient, string url, object? request, string? identifier, bool authenticated, Action<DataEvent<T>> dataHandler, CancellationToken ct) protected virtual async Task<CallResult<UpdateSubscription>> SubscribeAsync<T>(SocketApiClient apiClient, string url, object? request, string? identifier, bool authenticated, Action<DataEvent<T>> dataHandler, CancellationToken ct)
{ {
if (disposing)
return new CallResult<UpdateSubscription>(new InvalidOperationError("Client disposed, can't subscribe"));
SocketConnection socketConnection; SocketConnection socketConnection;
SocketSubscription subscription; SocketSubscription subscription;
var released = false; var released = false;
@ -277,6 +280,9 @@ namespace CryptoExchange.Net
/// <returns></returns> /// <returns></returns>
protected virtual async Task<CallResult<T>> QueryAsync<T>(SocketApiClient apiClient, string url, object request, bool authenticated) protected virtual async Task<CallResult<T>> QueryAsync<T>(SocketApiClient apiClient, string url, object request, bool authenticated)
{ {
if (disposing)
return new CallResult<T>(new InvalidOperationError("Client disposed, can't query"));
SocketConnection socketConnection; SocketConnection socketConnection;
var released = false; var released = false;
await semaphoreSlim.WaitAsync().ConfigureAwait(false); await semaphoreSlim.WaitAsync().ConfigureAwait(false);

View File

@ -69,7 +69,15 @@
/// <summary> /// <summary>
/// Data synced, order book is up to date /// Data synced, order book is up to date
/// </summary> /// </summary>
Synced Synced,
/// <summary>
/// Disposing
/// </summary>
Disposing,
/// <summary>
/// Disposed
/// </summary>
Disposed
} }
/// <summary> /// <summary>

View File

@ -506,9 +506,36 @@ namespace CryptoExchange.Net.OrderBook
} }
/// <summary> /// <summary>
/// Dispose the order book /// IDisposable implementation for the order book
/// </summary> /// </summary>
public abstract void Dispose(); public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Dispose method
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing)
{
Status = OrderBookStatus.Disposing;
_cts?.Cancel();
_queueEvent.Set();
// Clear queue
while (_processQueue.TryDequeue(out _)) { }
processBuffer.Clear();
asks.Clear();
bids.Clear();
AskCount = 0;
BidCount = 0;
Status = OrderBookStatus.Disposed;
}
/// <summary> /// <summary>
/// String representation of the top 3 entries /// String representation of the top 3 entries