Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## IDisposable pattern
- ```c#
- 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
- ```
- ## CompositeDisposable
- Instead of object.Dispose() use Disposable.Add(object)
- __Declaration__
- ```c#
- public MyClass
- {
- private CompositeDisposable Disposables { get; } = new CompositeDisposable();
- ```
- __When disposing the object__
- ```c#
- Disposables?.Dispose();
- ```
- ## DisposeWith
- __if you subscribe to class members DO NOT USE DisposeWith(Disposables)__
- ```c#
- this.WhenAnyValue(x => x).Subscribe(_ =>
- {
- });
- ```
- __any other subscripiton must be disposed__
- ```c#
- model.WhenAnyValue(x => x).Subscribe(_ =>
- {
- }).DisposeWith(Disposables);
- ```
- ## Event subsciptions
- __traditional pattern DO NOT USE__
- ```c#
- Global.ChaumianClient.StateUpdated += ChaumianClient_StateUpdated;
- if (Global.ChaumianClient != null)
- {
- Global.ChaumianClient.StateUpdated -= ChaumianClient_StateUpdated;
- }
- ```
- __the RX style__
- ```c#
- Observable.FromEventPattern(CoinList, nameof(CoinList.SelectionChanged)).Subscribe(_ => SetFeesAndTexts());
- Observable.FromEventPattern(
- Global.ChaumianClient,
- nameof(Global.ChaumianClient.StateUpdated))
- .ObserveOn(RxApp.MainThreadScheduler)
- .Subscribe(_ =>
- {
- RefreshSmartCoinStatus();
- }).DisposeWith(_disposables);
- ```
- ## Property definition and notifychange
- #### WRONG!
- ```c#
- private void Coin_PropertyChanged(object sender, PropertyChangedEventArgs e)
- {
- if (e.PropertyName == nameof(CoinViewModel.Unspent))
- {
- }
- }
- ```
- #### USE PropertyHelper!
- ```c#
- //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));
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement