Report cleanup failures and release Avalonia sample resources in order

This commit is contained in:
mika@stocksharp.com committed 2026-12-05 18:45:37 +03:00
1 parent 34080ab3d9
commit 9db6c0b9f9
25 files changed
+302 -407

No files matched your search

@@ -114,9 +114,9 @@ public partial class MainWindow : Window
() => Title = $"Simple moving average - {error.Message}",
DispatcherPriority.Background);
}
catch
catch (Exception displayError)
{
// The UI dispatcher can already be shutting down while the load is unwinding.
SampleShutdown.Report(new AggregateException(error, displayError));
}
}
}
@@ -146,7 +146,7 @@ public partial class MainWindow : Window
{
Opened -= OnOpened;
Closing -= OnClosing;
_loadCancellation.Dispose();
SampleShutdown.Dispose(_loadCancellation);
_closeApproved = true;
Close();
}
@@ -115,9 +115,9 @@ public partial class MainWindow : Window
() => Title = $"Complex Bollinger Bands - {error.Message}",
DispatcherPriority.Background);
}
catch
catch (Exception displayError)
{
// The UI dispatcher can already be shutting down while the load is unwinding.
SampleShutdown.Report(new AggregateException(error, displayError));
}
}
}
@@ -147,7 +147,7 @@ public partial class MainWindow : Window
{
Opened -= OnOpened;
Closing -= OnClosing;
_loadCancellation.Dispose();
SampleShutdown.Dispose(_loadCancellation);
_closeApproved = true;
Close();
}
@@ -123,9 +123,9 @@ public partial class MainWindow : Window
() => Title = $"Create your own indicator - {error.Message}",
DispatcherPriority.Background);
}
catch
catch (Exception displayError)
{
// The UI dispatcher can already be shutting down while the load is unwinding.
SampleShutdown.Report(new AggregateException(error, displayError));
}
}
}
@@ -155,7 +155,7 @@ public partial class MainWindow : Window
{
Opened -= OnOpened;
Closing -= OnClosing;
_loadCancellation.Dispose();
SampleShutdown.Dispose(_loadCancellation);
_closeApproved = true;
Close();
}
@@ -168,8 +168,10 @@ public partial class MainWindow : Window
{
await previousTask.ConfigureAwait(false);
}
catch
catch (Exception error)
{
if (error is not OperationCanceledException)
SampleShutdown.Report(error);
}
finally
{
@@ -298,8 +300,10 @@ public partial class MainWindow : Window
{
await previousTask.ConfigureAwait(false);
}
catch
catch (Exception error)
{
if (error is not OperationCanceledException)
SampleShutdown.Report(error);
}
finally
{
@@ -800,8 +804,9 @@ public partial class MainWindow : Window
{
await Dispatcher.UIThread.InvokeAsync(() => StatusText.Text = message, DispatcherPriority.Background);
}
catch
catch (Exception error)
{
SampleShutdown.Report(error);
}
}
@@ -824,16 +829,20 @@ public partial class MainWindow : Window
{
await _loadTask;
}
catch
catch (Exception error)
{
if (error is not OperationCanceledException)
SampleShutdown.Report(error);
}
try
{
await _securityScanTask;
}
catch
catch (Exception error)
{
if (error is not OperationCanceledException)
SampleShutdown.Report(error);
}
}
finally
@@ -848,8 +857,8 @@ public partial class MainWindow : Window
Chart.AnnotationSelected -= OnAnnotationSelected;
Chart.RegisterOrder -= OnRegisterOrder;
Closing -= OnClosing;
_loadCancellation.Dispose();
_securityScanCancellation.Dispose();
SampleShutdown.Dispose(_loadCancellation);
SampleShutdown.Dispose(_securityScanCancellation);
_closeApproved = true;
Close();
}
@@ -131,8 +131,10 @@ public partial class MainWindow : Window
{
await previousTask.ConfigureAwait(false);
}
catch
catch (Exception error)
{
if (error is not OperationCanceledException)
SampleShutdown.Report(error);
}
finally
{
@@ -592,8 +594,9 @@ public partial class MainWindow : Window
{
await Dispatcher.UIThread.InvokeAsync(() => Log(message), DispatcherPriority.Background);
}
catch
catch (Exception error)
{
SampleShutdown.Report(error);
}
}
@@ -621,23 +624,27 @@ public partial class MainWindow : Window
{
await _loadTask;
}
catch
catch (Exception error)
{
if (error is not OperationCanceledException)
SampleShutdown.Report(error);
}
try
{
await Task.WhenAll(transactions);
}
catch
catch (Exception error)
{
if (error is not OperationCanceledException)
SampleShutdown.Report(error);
}
}
finally
{
Closing -= OnClosing;
_loadCancellation.Dispose();
_transactionCancellation.Dispose();
SampleShutdown.Dispose(_loadCancellation);
SampleShutdown.Dispose(_transactionCancellation);
_closeApproved = true;
Close();
}
@@ -141,8 +141,10 @@ public partial class MainWindow : Window
{
await previousTask.ConfigureAwait(false);
}
catch
catch (Exception error)
{
if (error is not OperationCanceledException)
SampleShutdown.Report(error);
}
finally
{
@@ -380,8 +382,9 @@ public partial class MainWindow : Window
StatusText.Text = $"History load failed: {message}";
}, DispatcherPriority.Background);
}
catch
catch (Exception error)
{
SampleShutdown.Report(error);
}
}
@@ -413,8 +416,10 @@ public partial class MainWindow : Window
{
await _loadTask;
}
catch
catch (Exception error)
{
if (error is not OperationCanceledException)
SampleShutdown.Report(error);
}
finally
{
@@ -423,7 +428,7 @@ public partial class MainWindow : Window
PointerWheelChanged -= OnPointerWheelChanged;
PointerPressed -= OnPointerPressed;
Closing -= OnClosing;
_loadCancellation.Dispose();
SampleShutdown.Dispose(_loadCancellation);
_closeApproved = true;
Close();
}
@@ -339,9 +339,9 @@ public partial class MainWindow : Window
{
_logManager.Sources.Remove(source);
}
catch
catch (Exception error)
{
// Source removal must never skip owned session cleanup.
SampleShutdown.Report(error);
}
}
@@ -371,47 +371,25 @@ public partial class MainWindow : Window
{
await StopSessionAsync();
}
catch
catch (Exception error)
{
// Continue closing after every owned dependency had a disposal attempt.
SampleShutdown.Report(error);
}
finally
{
Closing -= OnClosing;
_uiEvents.Dispose();
try
{
_logManager.Dispose();
}
catch
{
// Continue the deterministic UI cleanup below.
}
finally
{
DisposeControl(_marketDepth);
DisposeControl(_orderGrid);
DisposeControl(_myTradeGrid);
DisposeControl(_statisticGrid);
DisposeControl(_monitor);
_lifetimeCancellation.Dispose();
_sessionGate.Dispose();
_closeApproved = true;
Close();
}
SampleShutdown.Dispose(_uiEvents);
SampleShutdown.Dispose(_marketDepth);
SampleShutdown.Dispose(_orderGrid);
SampleShutdown.Dispose(_myTradeGrid);
SampleShutdown.Dispose(_statisticGrid);
SampleShutdown.Dispose(_monitor);
SampleShutdown.Dispose(_lifetimeCancellation);
SampleShutdown.Dispose(_sessionGate);
SampleShutdown.Dispose(_logManager);
_closeApproved = true;
Close();
}
}
private static void DisposeControl(object control)
{
try
{
if (control is IDisposable disposable)
disposable.Dispose();
}
catch
{
// Window shutdown must continue so every remaining control is released.
}
}
}
@@ -339,9 +339,9 @@ public partial class MainWindow : Window
{
_logManager.Sources.Remove(source);
}
catch
catch (Exception error)
{
// Source removal must never skip owned session cleanup.
SampleShutdown.Report(error);
}
}
@@ -371,47 +371,25 @@ public partial class MainWindow : Window
{
await StopSessionAsync();
}
catch
catch (Exception error)
{
// Continue closing after every owned dependency had a disposal attempt.
SampleShutdown.Report(error);
}
finally
{
Closing -= OnClosing;
_uiEvents.Dispose();
try
{
_logManager.Dispose();
}
catch
{
// Continue the deterministic UI cleanup below.
}
finally
{
DisposeControl(_marketDepth);
DisposeControl(_orderGrid);
DisposeControl(_myTradeGrid);
DisposeControl(_statisticGrid);
DisposeControl(_monitor);
_lifetimeCancellation.Dispose();
_sessionGate.Dispose();
_closeApproved = true;
Close();
}
SampleShutdown.Dispose(_uiEvents);
SampleShutdown.Dispose(_marketDepth);
SampleShutdown.Dispose(_orderGrid);
SampleShutdown.Dispose(_myTradeGrid);
SampleShutdown.Dispose(_statisticGrid);
SampleShutdown.Dispose(_monitor);
SampleShutdown.Dispose(_lifetimeCancellation);
SampleShutdown.Dispose(_sessionGate);
SampleShutdown.Dispose(_logManager);
_closeApproved = true;
Close();
}
}
private static void DisposeControl(object control)
{
try
{
if (control is IDisposable disposable)
disposable.Dispose();
}
catch
{
// Window shutdown must continue so every remaining control is released.
}
}
}
@@ -339,9 +339,9 @@ public partial class MainWindow : Window
{
_logManager.Sources.Remove(source);
}
catch
catch (Exception error)
{
// Source removal must never skip owned session cleanup.
SampleShutdown.Report(error);
}
}
@@ -371,47 +371,25 @@ public partial class MainWindow : Window
{
await StopSessionAsync();
}
catch
catch (Exception error)
{
// Continue closing after every owned dependency had a disposal attempt.
SampleShutdown.Report(error);
}
finally
{
Closing -= OnClosing;
_uiEvents.Dispose();
try
{
_logManager.Dispose();
}
catch
{
// Continue the deterministic UI cleanup below.
}
finally
{
DisposeControl(_marketDepth);
DisposeControl(_orderGrid);
DisposeControl(_myTradeGrid);
DisposeControl(_statisticGrid);
DisposeControl(_monitor);
_lifetimeCancellation.Dispose();
_sessionGate.Dispose();
_closeApproved = true;
Close();
}
SampleShutdown.Dispose(_uiEvents);
SampleShutdown.Dispose(_marketDepth);
SampleShutdown.Dispose(_orderGrid);
SampleShutdown.Dispose(_myTradeGrid);
SampleShutdown.Dispose(_statisticGrid);
SampleShutdown.Dispose(_monitor);
SampleShutdown.Dispose(_lifetimeCancellation);
SampleShutdown.Dispose(_sessionGate);
SampleShutdown.Dispose(_logManager);
_closeApproved = true;
Close();
}
}
private static void DisposeControl(object control)
{
try
{
if (control is IDisposable disposable)
disposable.Dispose();
}
catch
{
// Window shutdown must continue so every remaining control is released.
}
}
}
@@ -220,9 +220,9 @@ public partial class MainWindow : Window
{
_logManager.Sources.Remove(session.Strategy);
}
catch
catch (Exception error)
{
// Source removal must never skip owned session cleanup.
SampleShutdown.Report(error);
}
try
@@ -261,37 +261,27 @@ public partial class MainWindow : Window
{
await StopSessionAsync();
}
catch
catch (Exception error)
{
// Continue closing after every owned dependency had a disposal attempt.
SampleShutdown.Report(error);
}
finally
{
Closing -= OnClosing;
_uiEvents.Dispose();
SampleShutdown.Dispose(_uiEvents);
try
{
_logManager.Dispose();
SampleShutdown.Dispose(_monitor);
}
catch
catch (Exception error)
{
// Continue the deterministic UI cleanup below.
}
finally
{
try
{
((IDisposable)_monitor).Dispose();
}
catch
{
// The application must still finish its closing handshake.
}
_lifetimeCancellation.Dispose();
_sessionGate.Dispose();
_closeApproved = true;
Close();
SampleShutdown.Report(error);
}
SampleShutdown.Dispose(_lifetimeCancellation);
SampleShutdown.Dispose(_sessionGate);
SampleShutdown.Dispose(_logManager);
_closeApproved = true;
Close();
}
}
}
@@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Ecng.Common;
using Ecng.ComponentModel;
using Ecng.Logging;
@@ -119,8 +120,7 @@ internal sealed class HistoryIndexSession : IAsyncDisposable
{
connector?.Dispose();
storageRegistry?.Dispose();
if (indexSecurity is IDisposable disposable)
disposable.Dispose();
indexSecurity.DoDispose();
throw;
}
}
@@ -262,18 +262,18 @@ public partial class MainWindow : Window
{
await StopSessionAsync();
}
catch
catch (Exception error)
{
SampleShutdown.Report(error);
}
finally
{
Closing -= OnClosing;
_uiEvents.Dispose();
_logManager.Dispose();
if (Monitor is IDisposable disposable)
disposable.Dispose();
_lifetimeCancellation.Dispose();
_sessionGate.Dispose();
SampleShutdown.Dispose(_uiEvents);
SampleShutdown.Dispose(Monitor);
SampleShutdown.Dispose(_lifetimeCancellation);
SampleShutdown.Dispose(_sessionGate);
SampleShutdown.Dispose(_logManager);
_closeApproved = true;
Close();
}
@@ -308,29 +308,25 @@ public partial class MainWindow : Window
{
await StopSessionAsync();
}
catch
catch (Exception error)
{
SampleShutdown.Report(error);
}
finally
{
Closing -= OnClosing;
_uiEvents.Dispose();
_logManager.Dispose();
DisposeControl(MarketDepth);
DisposeControl(OrderGrid);
DisposeControl(MyTradeGrid);
DisposeControl(StatisticGrid);
DisposeControl(Monitor);
_lifetimeCancellation.Dispose();
_sessionGate.Dispose();
SampleShutdown.Dispose(_uiEvents);
SampleShutdown.Dispose(MarketDepth);
SampleShutdown.Dispose(OrderGrid);
SampleShutdown.Dispose(MyTradeGrid);
SampleShutdown.Dispose(StatisticGrid);
SampleShutdown.Dispose(Monitor);
SampleShutdown.Dispose(_lifetimeCancellation);
SampleShutdown.Dispose(_sessionGate);
SampleShutdown.Dispose(_logManager);
_closeApproved = true;
Close();
}
}
private static void DisposeControl(object control)
{
if (control is IDisposable disposable)
disposable.Dispose();
}
}
@@ -343,8 +343,9 @@ public partial class MainWindow : Window
{
_logManager.Sources.Remove(source);
}
catch
catch (Exception error)
{
SampleShutdown.Report(error);
}
}
@@ -373,40 +374,30 @@ public partial class MainWindow : Window
{
await StopStrategyAsync();
}
catch
catch (Exception error)
{
SampleShutdown.Report(error);
}
finally
{
Closing -= OnClosing;
Opened -= OnOpened;
_connectorEvents.Dispose();
_uiEvents.Dispose();
SampleShutdown.Dispose(_connectorEvents);
SampleShutdown.Dispose(_uiEvents);
TryRemoveLogSource(_context.Connector);
TryDispose(_securityEditor);
TryDispose(_portfolioEditor);
TryDispose(_orderGrid);
TryDispose(_myTradeGrid);
TryDispose(_portfolioGrid);
TryDispose(_monitor);
TryDispose(_logManager);
TryDispose(_context);
TryDispose(_lifetimeCancellation);
TryDispose(_strategyGate);
SampleShutdown.Dispose(_securityEditor);
SampleShutdown.Dispose(_portfolioEditor);
SampleShutdown.Dispose(_orderGrid);
SampleShutdown.Dispose(_myTradeGrid);
SampleShutdown.Dispose(_portfolioGrid);
SampleShutdown.Dispose(_monitor);
await SampleShutdown.DisposeAsync(_context);
SampleShutdown.Dispose(_lifetimeCancellation);
SampleShutdown.Dispose(_strategyGate);
SampleShutdown.Dispose(_logManager);
_closeApproved = true;
Close();
}
}
private static void TryDispose(object value)
{
try
{
if (value is IDisposable disposable)
disposable.Dispose();
}
catch
{
}
}
}
@@ -359,8 +359,9 @@ public partial class MainWindow : Window
{
_logManager.Sources.Remove(source);
}
catch
catch (Exception error)
{
SampleShutdown.Report(error);
}
}
@@ -390,42 +391,32 @@ public partial class MainWindow : Window
{
await StopStrategyAsync();
}
catch
catch (Exception error)
{
SampleShutdown.Report(error);
}
finally
{
Closing -= OnClosing;
Opened -= OnOpened;
_connectorEvents.Dispose();
_uiEvents.Dispose();
SampleShutdown.Dispose(_connectorEvents);
SampleShutdown.Dispose(_uiEvents);
TryRemoveLogSource(_context.Connector);
TryDispose(_futureSecurity);
TryDispose(_stockSecurity);
TryDispose(_futurePortfolio);
TryDispose(_stockPortfolio);
TryDispose(_orderGrid);
TryDispose(_myTradeGrid);
TryDispose(_portfolioGrid);
TryDispose(_monitor);
TryDispose(_logManager);
TryDispose(_context);
TryDispose(_lifetimeCancellation);
TryDispose(_strategyGate);
SampleShutdown.Dispose(_futureSecurity);
SampleShutdown.Dispose(_stockSecurity);
SampleShutdown.Dispose(_futurePortfolio);
SampleShutdown.Dispose(_stockPortfolio);
SampleShutdown.Dispose(_orderGrid);
SampleShutdown.Dispose(_myTradeGrid);
SampleShutdown.Dispose(_portfolioGrid);
SampleShutdown.Dispose(_monitor);
await SampleShutdown.DisposeAsync(_context);
SampleShutdown.Dispose(_lifetimeCancellation);
SampleShutdown.Dispose(_strategyGate);
SampleShutdown.Dispose(_logManager);
_closeApproved = true;
Close();
}
}
private static void TryDispose(object value)
{
try
{
if (value is IDisposable disposable)
disposable.Dispose();
}
catch
{
}
}
}
@@ -827,7 +827,7 @@ public partial class MainWindow : Window
_isClosing = true;
_lifetimeCancellation.Cancel();
_refreshTimer.Stop();
_timerEvents.Dispose();
SampleShutdown.Dispose(_timerEvents);
_startButton.IsEnabled = false;
try
@@ -860,24 +860,24 @@ public partial class MainWindow : Window
{
Closing -= OnClosing;
Opened -= OnOpened;
_connectorEvents.Dispose();
SampleShutdown.Dispose(_connectorEvents);
UnsubscribeMarketData();
_uiEvents.Dispose();
SampleShutdown.Dispose(_uiEvents);
TryRemoveLogSource(Connector);
TryDispose(_assetCombo);
TryDispose(_optionCombo);
TryDispose(_portfolioEditor);
TryDispose(_positionChart);
TryDispose(_desk);
TryDispose(_smileChart);
TryDispose(_orderGrid);
TryDispose(_myTradeGrid);
TryDispose(_portfolioGrid);
TryDispose(_logManager);
TryDispose(_monitor);
TryDispose(_context);
TryDispose(_lifetimeCancellation);
TryDispose(_strategyGate);
SampleShutdown.Dispose(_assetCombo);
SampleShutdown.Dispose(_optionCombo);
SampleShutdown.Dispose(_portfolioEditor);
SampleShutdown.Dispose(_positionChart);
SampleShutdown.Dispose(_desk);
SampleShutdown.Dispose(_smileChart);
SampleShutdown.Dispose(_orderGrid);
SampleShutdown.Dispose(_myTradeGrid);
SampleShutdown.Dispose(_portfolioGrid);
SampleShutdown.Dispose(_monitor);
await SampleShutdown.DisposeAsync(_context);
SampleShutdown.Dispose(_lifetimeCancellation);
SampleShutdown.Dispose(_strategyGate);
SampleShutdown.Dispose(_logManager);
_closeApproved = true;
Close();
}
@@ -889,20 +889,10 @@ public partial class MainWindow : Window
{
_logManager.Sources.Remove(source);
}
catch
catch (Exception error)
{
SampleShutdown.Report(error);
}
}
private static void TryDispose(object value)
{
try
{
if (value is IDisposable disposable)
disposable.Dispose();
}
catch
{
}
}
}
@@ -347,8 +347,8 @@ public partial class MainWindow : Window
finally
{
Opened -= OnOpened;
_connectorEvents.Dispose();
_toolWindowEvents.Dispose();
SampleShutdown.Dispose(_connectorEvents);
SampleShutdown.Dispose(_toolWindowEvents);
foreach (var window in _toolWindows)
{
@@ -356,23 +356,24 @@ public partial class MainWindow : Window
{
window.Close();
}
catch
catch (Exception error)
{
SampleShutdown.Report(error);
}
}
TryDispose(_securitiesWindow);
TryDispose(_ordersWindow);
TryDispose(_portfoliosWindow);
TryDispose(_myTradesWindow);
TryDispose(_strategiesWindow);
_uiEvents.Dispose();
SampleShutdown.Dispose(_securitiesWindow);
SampleShutdown.Dispose(_ordersWindow);
SampleShutdown.Dispose(_portfoliosWindow);
SampleShutdown.Dispose(_myTradesWindow);
SampleShutdown.Dispose(_strategiesWindow);
SampleShutdown.Dispose(_uiEvents);
TryRemoveLogSource(Connector);
TryDispose(_logManager);
TryDispose(_monitor);
await TryDisposeAsync(_runtime);
TryDispose(_lifetimeCancellation);
SampleShutdown.Dispose(_monitor);
await SampleShutdown.DisposeAsync(_runtime);
SampleShutdown.Dispose(_lifetimeCancellation);
Closing -= OnClosing;
SampleShutdown.Dispose(_logManager);
_closeApproved = true;
Close();
}
@@ -384,31 +385,10 @@ public partial class MainWindow : Window
{
_logManager.Sources.Remove(source);
}
catch
catch (Exception error)
{
SampleShutdown.Report(error);
}
}
private static void TryDispose(object value)
{
try
{
if (value is IDisposable disposable)
disposable.Dispose();
}
catch
{
}
}
private static async ValueTask TryDisposeAsync(IAsyncDisposable value)
{
try
{
await value.DisposeAsync();
}
catch
{
}
}
}
@@ -630,9 +630,9 @@ public partial class MainWindow : Window
{
_logManager.Sources.Remove(source);
}
catch
catch (Exception error)
{
// Session disposal remains authoritative even if a listener rejects source removal.
SampleShutdown.Report(error);
}
}
@@ -680,48 +680,29 @@ public partial class MainWindow : Window
{
await StopSessionAsync();
}
catch
catch (Exception error)
{
// Continue the close handshake after every owned dependency had a disposal attempt.
SampleShutdown.Report(error);
}
finally
{
Closing -= OnClosing;
_uiEvents.Dispose();
try
{
_logManager.Dispose();
}
catch
{
// Continue releasing UI controls.
}
SampleShutdown.Dispose(_uiEvents);
foreach (var presentation in _presentations)
{
DisposeControl(presentation.Chart);
DisposeControl(presentation.Equity);
DisposeControl(presentation.Statistics);
DisposeControl(presentation.Position);
SampleShutdown.Dispose(presentation.Chart);
SampleShutdown.Dispose(presentation.Equity);
SampleShutdown.Dispose(presentation.Statistics);
SampleShutdown.Dispose(presentation.Position);
}
_lifetimeCancellation.Dispose();
_sessionGate.Dispose();
SampleShutdown.Dispose(_lifetimeCancellation);
SampleShutdown.Dispose(_sessionGate);
SampleShutdown.Dispose(_logManager);
_closeApproved = true;
Close();
}
}
private static void DisposeControl(object control)
{
try
{
if (control is IDisposable disposable)
disposable.Dispose();
}
catch
{
// A single control must not prevent the rest of the window from closing.
}
}
}
@@ -142,8 +142,7 @@ public partial class MainWindow : Window
_generation++;
_runEvents?.Dispose();
_runEvents = null;
if (createdRun is not null)
await createdRun.DisposeAsync();
await createdRun.DoDisposeAsync();
createdCancellation?.Dispose();
SetRunningState(false, "Ready");
}
@@ -365,19 +364,20 @@ public partial class MainWindow : Window
{
await _runTask;
}
catch
catch (Exception error)
{
if (error is not OperationCanceledException)
SampleShutdown.Report(error);
}
await _runGate.WaitAsync();
try
{
if (_run is not null)
await _run.DisposeAsync();
await _run.DoDisposeAsync();
_run = null;
_runEvents?.Dispose();
SampleShutdown.Dispose(_runEvents);
_runEvents = null;
_runCancellation?.Dispose();
SampleShutdown.Dispose(_runCancellation);
_runCancellation = null;
}
finally
@@ -389,9 +389,9 @@ public partial class MainWindow : Window
{
Closing -= OnClosing;
Statistics.Dispose();
_uiEvents.Dispose();
_lifetimeCancellation.Dispose();
_runGate.Dispose();
SampleShutdown.Dispose(_uiEvents);
SampleShutdown.Dispose(_lifetimeCancellation);
SampleShutdown.Dispose(_runGate);
_closeApproved = true;
Close();
}
@@ -381,8 +381,7 @@ internal sealed class OptimizationRun : IAsyncDisposable
{
var connector = strategy.Connector;
strategy.Dispose();
if (connector is IDisposable disposable)
disposable.Dispose();
connector.DoDispose();
}
}
@@ -692,7 +692,7 @@ public partial class MainWindow : Window
_securityPicker.MarketDataProvider = null;
TryRemoveLogSource(connector);
TryDisconnect(connector);
TryDispose(connector);
SampleShutdown.Dispose(connector);
}
private void TryRemoveLogSource(ILogSource source)
@@ -701,8 +701,9 @@ public partial class MainWindow : Window
{
_logManager.Sources.Remove(source);
}
catch
catch (Exception error)
{
SampleShutdown.Report(error);
}
}
@@ -729,28 +730,30 @@ public partial class MainWindow : Window
{
await Task.WhenAll(pendingTasks);
}
catch
catch (Exception error)
{
if (error is not OperationCanceledException)
SampleShutdown.Report(error);
}
finally
{
Closing -= OnClosing;
Opened -= OnOpened;
DisposeEmulationConnector();
_realConnectorEvents.Dispose();
SampleShutdown.Dispose(_realConnectorEvents);
TryDisconnect(_realContext.Connector);
_uiEvents.Dispose();
SampleShutdown.Dispose(_uiEvents);
TryRemoveLogSource(_realContext.Connector);
TryDispose(_logManager);
TryDispose(_logControl);
TryDispose(_securityPicker);
TryDispose(_emulatedDepth);
TryDispose(_realDepth);
TryDispose(_portfolioGrid);
TryDispose(_orderGrid);
TryDispose(_myTradeGrid);
TryDispose(_realContext);
TryDispose(_lifetimeCancellation);
SampleShutdown.Dispose(_logControl);
SampleShutdown.Dispose(_securityPicker);
SampleShutdown.Dispose(_emulatedDepth);
SampleShutdown.Dispose(_realDepth);
SampleShutdown.Dispose(_portfolioGrid);
SampleShutdown.Dispose(_orderGrid);
SampleShutdown.Dispose(_myTradeGrid);
await SampleShutdown.DisposeAsync(_realContext);
SampleShutdown.Dispose(_lifetimeCancellation);
SampleShutdown.Dispose(_logManager);
_closeApproved = true;
Close();
}
@@ -774,8 +777,9 @@ public partial class MainWindow : Window
{
connector.UnSubscribe(subscription);
}
catch
catch (Exception error)
{
SampleShutdown.Report(error);
}
}
@@ -786,20 +790,10 @@ public partial class MainWindow : Window
if (connector?.ConnectionState != ConnectionStates.Disconnected)
connector?.Disconnect();
}
catch
catch (Exception error)
{
SampleShutdown.Report(error);
}
}
private static void TryDispose(object value)
{
try
{
if (value is IDisposable disposable)
disposable.Dispose();
}
catch
{
}
}
}
@@ -45,18 +45,8 @@ public partial class MainWindow : Window
_disposed = true;
Opened -= OnOpened;
Closed -= OnClosed;
TryDispose(_workspace);
TryDispose(_context);
SampleShutdown.Dispose(_workspace);
SampleShutdown.Dispose(_context);
}
private static void TryDispose(IDisposable disposable)
{
try
{
disposable?.Dispose();
}
catch
{
}
}
}
@@ -62,32 +62,17 @@ public partial class MainWindow : Window
// the opening has to be over before what it opens is released; its failure is reported by the opening itself
await Task.WhenAny(_opening);
Opened -= OnOpened;
TryDispose(_workspace);
await TryDisposeAsync(_runtime);
_closeApproved = true;
Close();
}
private static void TryDispose(IDisposable disposable)
{
try
{
disposable?.Dispose();
Opened -= OnOpened;
SampleShutdown.Dispose(_workspace);
await SampleShutdown.DisposeAsync(_runtime);
}
catch
finally
{
_closeApproved = true;
Close();
}
}
private static async ValueTask TryDisposeAsync(IAsyncDisposable disposable)
{
try
{
await disposable.DisposeAsync();
}
catch
{
}
}
}
@@ -488,8 +488,7 @@ internal partial class AdvancedConnectorWorkspace : UserControl, IDisposable
{
try
{
if (value is IDisposable disposable)
disposable.Dispose();
value.DoDispose();
}
catch
{
+54
View File
@@ -0,0 +1,54 @@
namespace StockSharp.Samples;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Ecng.Common;
using Ecng.Logging;
internal static class SampleShutdown
{
public static void Report(Exception error)
{
try
{
if (LogManager.Instance?.Application is { } application)
{
application.AddErrorLog(error);
return;
}
}
catch (Exception loggingError)
{
Trace.TraceError(new AggregateException(error, loggingError).ToString());
return;
}
Trace.TraceError(error.ToString());
}
public static void Dispose(object value)
{
try
{
value.DoDispose();
}
catch (Exception error)
{
Report(error);
}
}
public static async ValueTask DisposeAsync(object value)
{
try
{
await value.DoDisposeAsync();
}
catch (Exception error)
{
Report(error);
}
}
}