public class MyClass : IDisposable
#region IDisposable Support
private volatile bool _disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
// TODO: dispose managed state (managed objects).
}
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
// TODO: set large fields to null.
_disposedValue = true;
}
}
public void Dispose()
{
Dispose(true);
}
#endregion IDisposable Support
Instead of object.Dispose() use Disposable.Add(object)
Declaration
public MyClass
{
private CompositeDisposable Disposables { get; } = new CompositeDisposable();
When disposing the object
Disposables?.Dispose();
if you subscribe to class members DO NOT USE DisposeWith(Disposables)
this.WhenAnyValue(x => x).Subscribe(_ =>
{
});
any other subscripiton must be disposed
model.WhenAnyValue(x => x).Subscribe(_ =>
{
}).DisposeWith(Disposables);
traditional pattern DO NOT USE
Global.ChaumianClient.StateUpdated += ChaumianClient_StateUpdated;
if (Global.ChaumianClient != null)
{
Global.ChaumianClient.StateUpdated -= ChaumianClient_StateUpdated;
}
the RX style
Observable.FromEventPattern(CoinList, nameof(CoinList.SelectionChanged)).Subscribe(_ => SetFeesAndTexts());
Observable.FromEventPattern(
Global.ChaumianClient,
nameof(Global.ChaumianClient.StateUpdated))
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(_ =>
{
RefreshSmartCoinStatus();
}).DisposeWith(_disposables);
private void Coin_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(CoinViewModel.Unspent))
{
}
}
//define class member
private ObservableAsPropertyHelper<bool> _confirmed;
//initilize PropertyHelper
_confirmed = Model.WhenAnyValue(x => x.Confirmed).ToProperty(this, x => x.Confirmed).DisposeWith(_disposables);
//if invoked from other thread
_confirmed = Model.WhenAnyValue(x => x.Confirmed).ToProperty(this, x => x.Confirmed, scheduler:RxApp.MainThreadScheduler).DisposeWith(_disposables);
//if we need to do something on change
this.WhenAnyValue(x => x.Confirmed, x => x.CoinJoinInProgress, x => x.Confirmations).Subscribe(_ => RefreshSmartCoinStatus());
Task.Run(async () => { while (true) { if (_notificationQueue.Any() && !_timer.IsEnabled) { var notification = _notificationQueue.Dequeue();
await Dispatcher.UIThread.InvokeAsync(()=>DisplayNotification(notification));
}
await Task.Delay(TimeSpan.FromSeconds(0.1));
}
});
Can I suggest an order to classes (at least viewmodels)
?