Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. ## IDisposable pattern
  2. ```c#
  3. public class MyClass : IDisposable
  4.  
  5. #region IDisposable Support
  6.  
  7. private volatile bool _disposedValue = false; // To detect redundant calls
  8.  
  9. protected virtual void Dispose(bool disposing)
  10. {
  11. if (!_disposedValue)
  12. {
  13. if (disposing)
  14. {
  15. // TODO: dispose managed state (managed objects).
  16. }
  17.  
  18. // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
  19. // TODO: set large fields to null.
  20.  
  21. _disposedValue = true;
  22. }
  23. }
  24.  
  25. public void Dispose()
  26. {
  27. Dispose(true);
  28. }
  29.  
  30. #endregion IDisposable Support
  31. ```
  32.  
  33. ## CompositeDisposable
  34.  
  35. Instead of object.Dispose() use Disposable.Add(object)
  36.  
  37. __Declaration__
  38. ```c#
  39. public MyClass
  40. {
  41. private CompositeDisposable Disposables { get; } = new CompositeDisposable();
  42. ```
  43.  
  44. __When disposing the object__
  45. ```c#
  46. Disposables?.Dispose();
  47. ```
  48.  
  49. ## DisposeWith
  50.  
  51. __if you subscribe to class members DO NOT USE DisposeWith(Disposables)__
  52.  
  53. ```c#
  54. this.WhenAnyValue(x => x).Subscribe(_ =>
  55. {
  56.  
  57. });
  58. ```
  59.  
  60. __any other subscripiton must be disposed__
  61. ```c#
  62. model.WhenAnyValue(x => x).Subscribe(_ =>
  63. {
  64.  
  65. }).DisposeWith(Disposables);
  66. ```
  67.  
  68. ## Event subsciptions
  69.  
  70. __traditional pattern DO NOT USE__
  71.  
  72. ```c#
  73. Global.ChaumianClient.StateUpdated += ChaumianClient_StateUpdated;
  74.  
  75. if (Global.ChaumianClient != null)
  76. {
  77. Global.ChaumianClient.StateUpdated -= ChaumianClient_StateUpdated;
  78. }
  79. ```
  80. __the RX style__
  81. ```c#
  82. Observable.FromEventPattern(CoinList, nameof(CoinList.SelectionChanged)).Subscribe(_ => SetFeesAndTexts());
  83.  
  84. Observable.FromEventPattern(
  85. Global.ChaumianClient,
  86. nameof(Global.ChaumianClient.StateUpdated))
  87. .ObserveOn(RxApp.MainThreadScheduler)
  88. .Subscribe(_ =>
  89. {
  90. RefreshSmartCoinStatus();
  91. }).DisposeWith(_disposables);
  92. ```
  93.  
  94. ## Property definition and notifychange
  95.  
  96. #### WRONG!
  97. ```c#
  98. private void Coin_PropertyChanged(object sender, PropertyChangedEventArgs e)
  99. {
  100. if (e.PropertyName == nameof(CoinViewModel.Unspent))
  101. {
  102. }
  103. }
  104. ```
  105.  
  106. #### USE PropertyHelper!
  107. ```c#
  108. //define class member
  109. private ObservableAsPropertyHelper<bool> _confirmed;
  110.  
  111. //initilize PropertyHelper
  112. _confirmed = Model.WhenAnyValue(x => x.Confirmed).ToProperty(this, x => x.Confirmed).DisposeWith(_disposables);
  113.  
  114. //if invoked from other thread
  115. _confirmed = Model.WhenAnyValue(x => x.Confirmed).ToProperty(this, x => x.Confirmed, scheduler:RxApp.MainThreadScheduler).DisposeWith(_disposables);
  116.  
  117. //if we need to do something on change
  118. this.WhenAnyValue(x => x.Confirmed, x => x.CoinJoinInProgress, x => x.Confirmations).Subscribe(_ => RefreshSmartCoinStatus());
  119. ```
  120.  
  121.  
  122. Task.Run(async () =>
  123. {
  124. while (true)
  125. {
  126. if (_notificationQueue.Any() && !_timer.IsEnabled)
  127. {
  128. var notification = _notificationQueue.Dequeue();
  129.  
  130. await Dispatcher.UIThread.InvokeAsync(()=>DisplayNotification(notification));
  131. }
  132. await Task.Delay(TimeSpan.FromSeconds(0.1));
  133. }
  134. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement